如何将Dio的部分响应转换为Flutter/Dat中的变量



使用Dio,我向网站提出了一个特定的请求:

final response = await Dio().get(
…
print(response); //Just to see my response

以下是控制台的响应:

I/flutter ( 4594): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","Data":"11.5","App":"The Best App","Current":"98789fg43"}}]}

我对这些数据感到满意。它们是正确的。对于进一步的工作,我只需要recrNhzWnPdiSFaCs值。我想把它传递给一个变量,该变量将在代码的另一部分中使用。怎么做?

Edit1.使用var decodedString = json.decode(response.data.toString());不会在代码中导致任何错误。但在启动时,控制台中会显示以下内容:

I/flutter ( 4594): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","First":"11.5","App":"The Best App","Current":"98789fg43"}}]}
E/flutter ( 4594): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 2)
E/flutter(4594): {records: [{id: recrNhzWnPdiSFaCs, createdTime: 2022-07-28T15:45:23.000Z, f...
E/flutter ( 4594): ^
E/flutter ( 4594):
E/flutter ( 4594): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
E/flutter ( 4594): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
E/flutter ( 4594): #2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
E/flutter ( 4594): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 4594): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter(4594): #5 _MainWidgetState.airtableFind(package:example/main.dart:214:32)
E/flutter ( 4594): <asynchronous suspension>
E/flutter ( 4594):

这是整个代码,以防我在某个地方犯了错误:

//Airtable (find a record)
void airtableFind() async {
try {
final response = await Dio().get(
'https://api.airtable.com/v0/'+projectBase+'/'+recordName,
queryParameters: {
// 'filterByFormula': 'SEARCH("Cactus",{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
'filterByFormula': 'SEARCH('+'"'+testName+'"'+',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
// 'filterByFormula': 'SEARCH('+testName+',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
},
options: Options(
contentType: 'Application/json',
headers: {
'Authorization': 'Bearer'+' '+apiKey,
'Accept': 'Application/json',
},
),
);
// TODO: Whatever you want to do with the response. A good practice is to transform it into models and than work with them
print(response);
// String fromString = response;
// String response1 = '{"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","Data":"11.5","App":"The Best App","Current":"98789fg43"}}]}';
var decodedString = json.decode(response.data.toString());
// final decodedJSON = json.decode(response.body);
// final decodedJSON = json.decode(utf8.decode(response.bodyBytes));
print(decodedString['records'][0]['id']);
String id = decodedString['records'][0]['id'];

} on DioError catch (e) {
// TODO: Error handling
if (e.response != null) {
// print(e.response.data);
print(e);
} else {
// print(e.request);
print(e.message);
}
}
}

Edit2.当我使用final decodedString = json.decode(response.data);时,我在控制台中看到以下错误:

I/flutter( 4289): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","First":"11.5","App":"The Best App","Current":"98789fg43"}}]}
E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
E/flutter(4289): #0 _MainWidgetState.airtableFind(package:example/main.dart:217:50)
E/flutter ( 4289): <asynchronous suspension>

当我使用var decodedString = json.decode(response.data.toString());时,控制台中会出现以下错误:

I/flutter( 4289): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","First":"11.5","App":"The Best App","Current":"98789fg43"}}]}
E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 2)
E/flutter(4289): {records: [{id: recrNhzWnPdiSFaCs, createdTime: 2022-07-28T15:45:23.000Z, f...
E/flutter ( 4289): ^
E/flutter ( 4289):
E/flutter ( 4289): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
E/flutter ( 4289): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
E/flutter ( 4289): #2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
E/flutter ( 4289): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 4289): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter(4289): #5 _MainWidgetState.airtableFind(package:example/main.dart:217:32)
E/flutter ( 4289): <asynchronous suspension>

您可以使用json.decode将响应转换为json值。然后,您可以使用等相应的密钥获取其值

String response = '{"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","Data":"11.5","App":"The Best App","Current":"98789fg43"}}]}';
var decodedString  = json.decode(response);
print(decodedString['records'][0]['id']);
//Output : recrNhzWnPdiSFaCs

不要错过导入

import 'dart:convert';

您可以通过解码JSON来实现您的目的。

final decodedJSON = json.decode(response.data);

,如果您的响应包含UTF-8编码(例如有波斯字符(,则可以使用:

final decodedJSON = json.decode(utf8.decode(response.bodyBytes));

dio主体名称是数据,因此解码如下

final decodedJSON = json.decode(response.data);

最新更新