[英]Flutter: How to map a nested json object
I'm new to flutter and am trying to get data into a list view in my app.我是 flutter 的新手,我正在尝试将数据放入我的应用程序的列表视图中。
I have this json object structure retrieved from a webserver:我从网络服务器检索到这个 json object 结构:
{
"item_data1":
{
"index": 0,
"age": 39,
"picture": "http://placehold.it/32x32",
"name": "Sospeter",
"email": "sospeter@123.com"
},
"item_data2":
{
"index": 1,
"age": 49,
"picture": "http://placehold.it/32x32",
"name": "Eric",
"email": "mericr@123.com"
}
}
NB: The keys "item_data1" and "item_data2" are dynamic and cannot be pre-determined in code.注意:键“item_data1”和“item_data2”是动态的,不能在代码中预先确定。
You may use the following link to get the json: http://www.json-generator.com/api/json/get/bUAhSFmhNK?indent=2您可以使用以下链接获取 json: http://www.json-generator.com/api/json/get/bUAhSFmhNK?indent=2
My intention is to dart:convert the json in my project.我的意图是dart:convert json 在我的项目中。
So far I have this:到目前为止,我有这个:
Future<List<User>> _getUsers() async {
var url = "http://www.json-generator.com/api/json/get/bUAhSFmhNK?indent=2"; //Obj
var data = await http.get(url);
var jsonData = json.decode(data.body);
//print(jsonData.toString()); <-- returns data
List<User> users = [];
for(var u in jsonData){ <!-- breaks
User user = User(u["index"], u["age"], u["name"], u["email"], u["picture"]);
users.add(user);
}
//print(users.length);
return users;
}
How can I safely get the "name", "email",...etc?如何安全地获取“姓名”、“电子邮件”等?
I'm stumped.我难住了。 I'll appreciate any help I can get:)
我会很感激我能得到的任何帮助:)
You can use the Object from json approach.您可以使用 json 方法中的 Object。
https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51 https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
It consist of defined a function which will parse the icoming json and build your object, setting null when properties missing.它由定义的 function 组成,它将解析即将到来的 json 并构建您的 object,当缺少属性时设置 Z37A6259CC0C1DAE2998
Try changing尝试改变
var jsonData = json.decode(data.body);
to至
var jsonData = json.decode(data.body) as Map<String, dynamic>;
Then use然后使用
jsonData.forEach((k, v) => print("Key : $k, Value : $v"));
to see what you have.看看你有什么。
You should then be able to populate your User object and add it to the list with the following code:然后,您应该能够填充您的用户 object 并使用以下代码将其添加到列表中:
User user = User(v["index"], v["age"], v["name"], v["email"], v["picture"]);
users.add(user);
(I am assuming your User model is set up to support this) (我假设您的用户 model 设置为支持此功能)
Another way:另一种方式:
import 'package:http/http.dart' as http;
import 'package:json_helpers/json_helpers.dart';
Future<void> main() async {
final users = await _getUsers();
print(users.first.name);
}
Future<List<User>> _getUsers() async {
final url = 'http://www.json-generator.com/api/json/get/bUAhSFmhNK?indent=2';
final response = await http.get(Uri.parse(url));
final map = response.body.jsonMap((e) => User.fromJson(e));
return map.values.toList();
}
class User {
int age;
int index;
String email;
String name;
String picture;
User({this.age, this.email, this.index, this.name, this.picture});
factory User.fromJson(Map<String, dynamic> json) {
return User(
age: json['age'] as int,
email: json['email'] as String,
index: json['index'] as int,
name: json['name'] as String,
picture: json['picture'] as String,
);
}
}
Output: Output:
Eric
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.