json - 如何从服务器解码此响应?我被困住了,响应的“数据”在地图中
问题描述
我来自服务器的 json 响应
{"success":1,"error":[],"data":{"38":{"address_id":"38","firstname":"Raj","lastname":"s","company":"","address_1":"aaaa","address_2":"","postcode":"966666","city":"aa","zone_id":"1234","zone":"Kerewan","zone_code":"KE","country_id":"0","country":"","iso_code_2":"","iso_code_3":"","address_format":"","custom_field":null},"37":{"address_id":"37","firstname":"Raj","lastname":"s","company":"","address_1":"4 kk\t","address_2":"","postcode":"56774\t","city":"Chennai\t","zone_id":"1234","zone":"Kerewan","zone_code":"KE","country_id":"0","country":"","iso_code_2":"","iso_code_3":"","address_format":"","custom_field":null},}}
我的最小代码
List<Address> listAddress;
Future<List<Address>> getAddresList()async {
List<Address> listAddress;
{
try {
var response = await http.post(
"URL",
headers: {"content-type": "application/json", "cookie": cookie});
List<Address> list = [];
if (response.statusCode == 200) {
var data=convert.jsonDecode(response.body);
for (var item in convert.jsonDecode(response.body)) {
list.add(AddressOpencart.fromJson(item) as Address);
}
}
setState(() {
listAddress = list;
print("DDll"+listAddress.toString());
});
} catch (err,trace) {
print(trace.toString());
print(err.toString());
rethrow;
}
}
}
我的地址模型
Address.fromOpencartJson(Map<String, dynamic> json) {
try {
firstName = json['firstname'];
lastName = json['lastname'];
street = json['address_1'];
city = json['city'];
state = json['zone'];
country = json['country'];
phoneNumber = json['phone'];
zipCode = json['postcode'];
} catch (e) {
print(e.toString());
}
}
解决方案
您必须在 API 响应中修复您的数据参数。它应该是一个对象数组。
{
"success": 1,
"error": [
],
"data": [
{
"address_id": "38",
"firstname": "Raj",
"lastname": "s",
"company": "",
"address_1": "aaaa",
"address_2": "",
"postcode": "966666",
"city": "aa",
"zone_id": "1234",
"zone": "Kerewan",
"zone_code": "KE",
"country_id": "0",
"country": "",
"iso_code_2": "",
"iso_code_3": "",
"address_format": "",
"custom_field": null
},
{
"address_id": "37",
"firstname": "Raj",
"lastname": "s",
"company": "",
"address_1": "4 kk\t",
"address_2": "",
"postcode": "56774\t",
"city": "Chennai\t",
"zone_id": "1234",
"zone": "Kerewan",
"zone_code": "KE",
"country_id": "0",
"country": "",
"iso_code_2": "",
"iso_code_3": "",
"address_format": "",
"custom_field": null
}
]
}
现在,您的代码对我来说看起来不错,但几乎没有更改:
Future<List<Address>> getAddresList()async {
List<Address> listAddress;
try {
var response = await http.post(
"URL",
headers: {"content-type": "application/json", "cookie": cookie});
List<Address> list = List<Address>();
if (response.statusCode == 200) {
var data = jsonDecode(response.body);
for (var item in data["data"]) {
list.add(AddressOpencart.fromJson(item) as Address);
}
}
setState(() {
listAddress = list;
print("DDll"+listAddress.toString());
});
} catch (err,trace) {
print(trace.toString());
print(err.toString());
}
}
Address.fromOpencartJson(Map<String, dynamic> json) :
firstName = json['firstname'],
lastName = json['lastname'],
street = json['address_1'],
city = json['city'],
state = json['zone'],
country = json['country'],
phoneNumber = json['phone'],
zipCode = json['postcode'];
推荐阅读
- go - time.After 在指定时间后没有被触发
- verilog - 最大迭代次数/时钟周期
- vba - 使用 Option Explicit 的 MS Access VBA 给出“变量未定义”错误,但适用于不同的数据库
- machine-learning - Google 机器学习速成课程 MNIST 示例训练在本地 PC 上出现分歧
- javascript - 创建一个猜谜游戏,计算机猜测用户输入的数字
- jsonschema - 如何使用基于布尔值的 Jsonschema Draft 7 if-then-else?
- c# - Windows 的 .NET CLR 与 Linux 或 Mac OS 的不同吗?
- node.js - 有人可以建议一个单行正则表达式来解析带有 - 或 / 分隔符的字母数字和可选数字 id 吗?
- javascript - 向数组中的每个元素添加一个随机数
- python - pytest 使用 Makefile 和 make 命令