在我向服務器發出請求后,我得到了這個 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 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,它也不會加載到我的螢屏上。
有什么解決辦法嗎?
uj5u.com熱心網友回復:
試試這個
bodybytedecoded["image"][0]["src"] // here you change the index of image
uj5u.com熱心網友回復:
改變
bodybytedecoded["image"]["src"]
到
bodybytedecoded["images"][0]["src"]
uj5u.com熱心網友回復:
bodybytedecoded["image"][0]["id"] //為id
bodybytedecoded["image"][0]["src"] // 為 src
// 只列印資料到控制臺
print("${bodybytedecoded["image"][0]["id"]}");
//你有56043,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359699.html
