[英]how to decode complex json in dart/flutter?
在我向服务器发出请求后,我得到了这个 json:
[
{
"id": 56012,
"name": "The name",
"description": "<p>description</p>\n",
"regular_price": "45000",
"sale_price": "45000",
"tags": [],
"images": [
{
"id": 56043,
"src": "pic link",
}
],
},
]
我想解码它们并在我的屏幕上显示它们
所以我的 class model 是:
class ProductModel {
late int _id;
late String _name;
late String _regular_price;
late String _description;
late var _image;
ProductModel(this._id, this._name, this._regular_price, this._description,
this._image);
get image => _image;
set image(value) {
_image = value;
}
String get regular_price => _regular_price;
set regular_price(String value) {
_regular_price = value;
}
String get description => _description;
set description(String value) {
_description = value;
}
String get name => _name;
set name(String value) {
_name = value;
}
int get id => _id;
set id(int value) {
_id = value;
}
}
现在我已经像这样解码了这个 json:
var bodybytedecoded= jsonDecode(utf8.decode(response.bodyBytes));
var productItem = ProductModel(bodybytedecoded["id"], bodybytedecoded["name"], bodybytedecoded["regular_price"],bodybytedecoded["description"],bodybytedecoded["image"]["src"]);
我可以访问 id、name、description,但我不能在图像数组中使用 src,并且它不会加载到我的屏幕上。
有什么解决办法吗?
改变
bodybytedecoded["image"]["src"]
至
bodybytedecoded["images"][0]["src"]
试试这个
bodybytedecoded["image"][0]["src"] // here you change the index of image
bodybytedecoded["image"][0]["id"] //为id
bodybytedecoded["image"][0]["src"] // 用于 src
//只打印数据到控制台
print("${bodybytedecoded["image"][0]["id"]}");
//你有 56043,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.