我是新手,如何将这个JSON对象解析为listview?
// API响应
"code": 200,
"message": "OK",
"payload": {
"items": [
{
"package": {
"order": {
"orderer": "xxxx",
"cost": {
"amount": "0.0",
"currency": {
"code": "USD",
"name": "US Dollar",
"symbol": "$"
}
},
"order_number": "212121",
"tracking_code": "833939339",
"store": {
"name": "ABC",
"id": 2,
"created_at": "2019-01-21T16:05:45+00:00"
},
"id": 74,
"created_at": "2019-02-08T11:55:14+00:00"
},
"weight": {
"value": "12.9000",
"unit": {
"name": "Kilogram",
"symbol": "kg",
"base": "1.00000000",
"id": 1
}
},
"length": {
"value": "4.0000",
"unit": {
"name": "Foot",
"symbol": "ft",
"base": "0.30480000",
"id": 7
}
},
"height": {
"value": "34.4000",
"unit": {
"name": "Millimetre",
"symbol": "mm",
"base": "0.00100000",
"id": 4
}
},
"width": {
"value": "98.5600",
"unit": {
"name": "Centimetre",
"symbol": "cm",
"base": "0.01000000",
"id": 2
}
},
"handlings": [
{
"description": "description",
"rate": "0.0200",
"icon": "icon-hazadous",
"id": 1
},
{
"description": "none ",
"rate": "0.0500",
"icon": "icon-none",
"id": 2
}
],
"description": "Item description",
"id": 113,
"created_at": "2019-02-08T11:55:14+00:00"
},
"owner": {
"name": "Jo B",
"phone": "+1",
"blocked": false,
"activated": true,
"id": 98,
"created_at": "2019-01-21T16:05:46+00:00",
"updated_at": "2018-12-21T15:03:41+00:00"
},
"sender": {
"name": "A B C",
"phone": "+1",
"blocked": false,
"activated": true,
"id": 98,
"created_at": "2019-01-21T16:05:46+00:00",
"updated_at": "2018-12-21T15:03:41+00:00"
},
"creator": {
"name": "Jo B",
"phone": "+1",
"blocked": false,
"activated": true,
"id": 98,
"created_at": "2019-01-21T16:05:46+00:00",
"updated_at": "2018-12-21T15:03:41+00:00"
},
"source": {
"contact_name": "Some name",
"contact_phone": "+1",
"name": "Some name",
"longitude": "-0.0",
"latitude": "1.1",
"id": 113,
"created_at": "2019-02-08T11:55:14+00:00"
},
"destination": {
"contact_name": "xxxx",
"contact_phone": "323232232",
"name": "xxx ssss",
"longitude": "-0.0",
"latitude": "1.1",
"id": 113,
"created_at": "2019-02-08T11:55:14+00:00"
},
"fare": {
"amount": "3185.7377",
"currency": {
"code": "USD",
"name": "US Dollar",
"symbol": "$"
}
},
"type": {
"name": "some name",
"code": "01",
"commission": "0.0",
"id": 2,
"created_at": "2019-01-21T16:05:45+00:00"
},
"freight": {
"name": "Land",
"code": "Land",
"id": 1,
"created_at": "2019-01-21T16:05:45+00:00"
},
"status": {
"name": "Pending",
"code": "PENDING",
"id": 1,
"created_at": "2019-01-21T16:05:45+00:00"
},
"payment": {
"method": {
"name": "None",
"code": "None",
"id": 1,
"created_at": "2019-01-21T16:05:46+00:00"
},
"status": {
"name": "Unpaid",
"code": "UNPAID",
"id": 1,
"created_at": "2019-01-21T16:05:46+00:00"
},
"id": 113,
"created_at": "2019-02-08T11:55:14+00:00"
},
"id": 113,`enter code here`
"created_at": "2019-02-08T11:55:14+00:00"
],
"total": 34,
"offset": 34
}
}
Flutter异步HTTP请求以获取API数据,已打印JSON,但我似乎并未从JSON请求中创建列表视图。
Future<Null> _fetchData() async {
setState(() {
loading = true;
});
final response =
await http.get("url" , headers: {"someh header": "xxxx"});
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
// request prints here`enter code here`
print(data);
);
}
}
答案 0 :(得分:0)
签出this blog。我认为它以详细的步骤解释了您的问题的解决方案。 让我知道这是否适合您! 我认为博客中针对您的问题的重点是 JSON结构#4:具有列表的嵌套结构。
一旦您从上述步骤中获得了List
个需要显示的对象,便可以在ListView.builder
中使用此列表在应用程序中显示它们。
示例:
showList(List<Your_Model_Class_Name> list) {
return ListView.builder(
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
/*Your widget code goes here instead of the ListTile*/
return ListTile(
title: Text('${list[index].title}'),
);
}
);
}
您可以查看this link以获得完整的示例。让我知道是否有帮助!
答案 1 :(得分:0)
您的解码不正确
final dynamic data = json.decode(response.body)
现在,当您打印时,它会告诉您您想要看的东西。