Flutter - Json 对象数组
Posted
技术标签:
【中文标题】Flutter - Json 对象数组【英文标题】:Flutter - Json array of objects 【发布时间】:2019-12-21 19:57:27 【问题描述】:我将如何解析嵌套的对象数组,例如下面的对象。
"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": ""
,
]
我目前正在进行 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');
然后我有一个如下位置类:
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
);
我希望能够使用 listview builder 检索上述所有位置,并为每个位置创建一个 listtile。
如何正确解析 JSON?
【问题讨论】:
return Location.fromJson(responseJson.data);
【参考方案1】:
关注这个 url 你会更多地了解 json 解析。 https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
这里是你正在寻找的代码,我在资产中使用了静态 json,所以你必须用你的 resonse 替换。
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);
数据模型
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']
);
【讨论】:
使用内置值类型来轻松对复杂对象进行 JSON 序列化。以上是关于Flutter - Json 对象数组的主要内容,如果未能解决你的问题,请参考以下文章
在 Flutter 中将 JSON 转换为类似于 android 的 POJO(对象)