【发布时间】:2020-09-02 14:45:45
【问题描述】:
我收到来自我的 API 的动态响应,但我不知道如何像字段正在更改的内部数据一样检索它:
{
"status": "ERROR",
"data": {
"email-common": "This email already exists."
}
}
另外一次可能是这样的:
{
"status": "ERROR",
"data": {
"password": "passwords do not match"
}
}
我知道 Java 中的代码,但在颤振中我不知道该怎么做。可以从这个 Java 代码中得到想法
JSONObject data = result.getJSONObject("data");
Iterator<String> keys = data.keys();
while (keys.hasNext()) {
String key = keys.next();
Log.d("myTag", data.get(key).toString());
}
如果我这样做是为了密码,那么它会抛出错误,对于另一个我的问题是我可以检索它的任何值(动态检索)
Future<Map> getJson() async {
String apiUrl = 'http://3.127.255.230/rest_ci/api/customers';
Map<String,String> headers = {'Content-Type':'application/json','Authorization':'mykeyyyy'};
final msg = jsonEncode({
"username":"tesstestt123",
"email":"khansdf@gmail.com",
"password":"testtest",
"confirm_password":"testtest",
});
http.Response response = await http
.post(apiUrl,
headers: headers,
body: msg, );
return json.decode(response.body); // returns a List type
}
void khan() async {
getJson();
String _body = "";
Map _data = await getJson();
_body = (_data['status']);
print(_data);
if (_body == 'ERROR') {
setState(() {
_isLoading = false;
});
print("$_data");
showDialog(
context: context,
builder: (c) {
return AlertDialog(
title: Text("oops"),
content: Text(_data['data']['password']),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Close"),
)
],
);
});
}
【问题讨论】: