Bol*_*lie 5 arrays json object flutter
我将如何解析嵌套的对象数组,如下所示。
{
"status": "success",
"code": 200,
"message": "The request was successful",
"data": [
{
"name": "Abu Dhabi",
"id": 4139,
"parent": 5153,
"type": "city",
"imageURL": ""
},
{
"name": "Croatia",
"id": 5037,
"parent": 6886,
"type": "country",
"imageURL": ""
},
]
}
Run Code Online (Sandbox Code Playgroud)
我目前正在进行 API 调用,该调用返回的数据格式如上所示。
我的api调用如下:
Future<Location> getLocations() async {
final response =
await http.get('$endpoint/locations', headers: authenticatedHeader);
if (response.statusCode == 200) {
final responseJson = json.decode(response.body);
// If server returns an OK response, parse the JSON.
return Location.fromJson(responseJson);
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post');
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个位置类如下:
class Location {
String name;
int id;
int parent;
String type;
String imageURL;
Location(
{this.name, this.id, this.parent, this.type, this.imageURL});
factory Location.fromJson(Map<String, dynamic> json) =>
_locationFromJson(json);
}
Location _locationFromJson(Map<String, dynamic> json) {
return Location(
name: json['name'] as String,
id: json['id'] as int,
parent: json['parent'] as int,
type: json['type'] as String,
imageURL: json['imageURL'] as String
);
}
Run Code Online (Sandbox Code Playgroud)
我希望能够使用列表视图生成器检索上面的所有位置,并为每个位置创建一个列表图块。
如何正确解析 JSON?
Follor this url you will more idea about json parse. https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
这是您正在寻找的代码,我在资产中使用了静态 json,因此您必须替换为您的响应。
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:flutter_json/model/location_model.dart';
Future<String> _loadlocationAsset() async {
return await rootBundle.loadString('assets/location.json');
}
Future loadLocation() async {
String jsonLocation = await _loadlocationAsset();
final jsonResponse = json.decode(jsonLocation);
LocationData location = new LocationData.fromJson(jsonResponse);
print(location.data[0].name);
}
Run Code Online (Sandbox Code Playgroud)
数据模型
class LocationData {
final int code;
final String status;
final String message;
final List<Data> data;
LocationData({this.code, this.status,this.message, this.data});
factory LocationData.fromJson(Map<String, dynamic> parsedJson){
var list = parsedJson['data'] as List;
print(list.runtimeType);
List<Data> dataList = list.map((i) => Data.fromJson(i)).toList();
return LocationData(
code: parsedJson['code'],
status: parsedJson['status'],
message: parsedJson['message'],
data: dataList
);
}
}
class Data {
final int id;
final int parent;
final String name;
final String type;
final String imageURL;
Data({this.id, this.parent,this.name,this.type,this.imageURL});
factory Data.fromJson(Map<String, dynamic> parsedJson){
return Data(
id:parsedJson['id'],
parent:parsedJson['parent'],
name:parsedJson['name'],
type:parsedJson['type'],
imageURL:parsedJson['imageURL']
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18999 次 |
| 最近记录: |