Flutter 如何在flatter中解析嵌套的JSON数组?

Flutter 如何在flatter中解析嵌套的JSON数组?,flutter,dart,Flutter,Dart,我正在使用json_模型插件制作PODO类。我已成功解析主数组。但我不能使用该插件将包含Map数组的“images”键放入PODO类中,也不能解析数据 下面是我要分析的数组 [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "su

我正在使用json_模型插件制作PODO类。我已成功解析主数组。但我不能使用该插件将包含Map数组的“images”键放入PODO类中,也不能解析数据

下面是我要分析的数组

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    },
    "images": [
      {
        "id": 11,
        "imageName": "xCh-rhy"
      },
      {
        "id": 31,
        "imageName": "fjs-eun"
      },
      {
        "id": 51,
        "imageName": "asd-fdg"
      },
      {
        "id": 71,
        "imageName": "zxc-cvb"
      },
      {
        "id": 91,
        "imageName": "qwe-hgj"
      }
    ]
  },
    ...
]
谢谢

在这里解析json数据

Future\u fetchUser()异步{
最后答复=
等待DefaultAssetBundle.of(context.loadString('users.json');
if(响应!=null){
List users=json.decode(response.toString());
返回users.map((user)=>users.fromJson(user)).toList();
}否则{
抛出异常('加载数据失败!');
}
}
正在尝试显示“图像”数据

FutureBuilder(
未来:_fetchUser(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasError){
返回中心(子项:Text('Error:${snapshot.Error}');
}else if(snapshot.hasData){
列表用户=snapshot.data;
返回ListView.separated(
itemCount:users.length,
itemBuilder:(构建上下文,int索引){
返回列表块(
标题:文本(“${users[index].username}”),
字幕:文本(“${users[index].images[index].imageName}”),
onTap:(){
Navigator.pushNamed(上下文,DetailsScreen.route,
参数:用户[索引];
},
);
},
separatorBuilder:(BuildContext上下文,int索引)=>
分隔符(),
);
}否则{
返回中心(子项:CircularProgressIndicator());
}
},
)

但是它显示了错误:索引超出范围:索引应该小于5:5

这是简单的对象,我不考虑数组。它只是内部对象模型。 您可以通过以下方式生成模型:

类自动生成{
int-id;
字符串名;
字符串用户名;
字符串电子邮件;
地址;
字符串电话;
字符串网站;
公司;
列出图像;
自动生成(
{this.id,
这个名字,
这个.username,
这是一封电子邮件,
这个地址,
这个电话,
本网站,
这家公司,
这是图像});
Autogenerated.fromJson(映射json){
id=json['id'];
name=json['name'];
username=json['username'];
email=json['email'];
地址=
json['address']!=null?新地址。fromJson(json['address']):null;
phone=json['phone'];
website=json['website'];
公司=
json['company']!=null?新公司。fromJson(json['company']):null;
if(json['images']!=null){
图像=新列表();
json['images'].forEach((v){
add(newimages.fromJson(v));
});
}
}
映射到JSON(){
最终地图数据=新地图();
数据['id']=this.id;
数据['name']=this.name;
数据['username']=this.username;
数据['email']=this.email;
if(this.address!=null){
data['address']=this.address.toJson();
}
数据['phone']=this.phone;
数据['website']=本网站;
如果(this.company!=null){
data['company']=this.company.toJson();
}
如果(this.images!=null){
data['images']=this.images.map((v)=>v.toJson()).toList();
}
返回数据;
}
}
班级地址{
弦街;;
弦乐组曲;
字符串城市;
字符串zipcode;
Geo;
地址({this.street,this.suite,this.city,this.zipcode,this.geo});
Address.fromJson(映射json){
street=json['street'];
suite=json['suite'];
city=json['city'];
zipcode=json['zipcode'];
geo=json['geo']!=null?新建geo.fromJson(json['geo']):null;
}
映射到JSON(){
最终地图数据=新地图();
数据['street']=this.street;
数据['suite']=this.suite;
数据['city']=本市;
数据['zipcode']=this.zipcode;
如果(this.geo!=null){
data['geo']=this.geo.toJson();
}
返回数据;
}
}
类Geo{
弦拉特;
管柱液化天然气;
Geo({this.lat,this.lng});
Geo.fromJson(映射json){
lat=json['lat'];
lng=json['lng'];
}
映射到JSON(){
最终地图数据=新地图();
数据['lat']=this.lat;
数据['lng']=this.lng;
返回数据;
}
}
阶级公司{
字符串名;
弦乐流行语;
字符串bs;
公司({this.name,this.catchPhrase,this.bs});
Company.fromJson(映射json){
name=json['name'];
catchPhrase=json['catchPhrase'];
bs=json['bs'];
}
映射到JSON(){
最终地图数据=新地图();
数据['name']=this.name;
数据['catchPhrase']=this.catchPhrase;
数据['bs']=this.bs;
返回数据;
}
}
类图像{
int-id;
字符串图像名称;
图像({this.id,this.imageName});
fromJson(映射json){
id=json['id'];
imageName=json['imageName'];
}
映射到JSON(){
最终地图数据=新地图();
数据['id']=this.id;
数据['imageName']=this.imageName;
返回数据;
}
}

建议的答案是可以的,但会出现一些错误(即使在链接中提供的自动转换器中)。例如:

不正常:

Geo.fromJson(Map<String, dynamic> json) {
    lat = json['lat'];
    lng = json['lng'];
}
Geo.fromJson(映射json){
lat=json['lat'];
lng=json['lng'];
}
好:

Geo.fromJson(映射json):
lat=json['lat'],
lng=json['lng'];

非常感谢你,兄弟!您引导我找到了解析json的最简单方法。你能给我引路吗
Geo.fromJson(Map<String, dynamic> json) {
    lat = json['lat'];
    lng = json['lng'];
}
Geo.fromJson(Map<String, dynamic> json):
    lat = json['lat'],
    lng = json['lng'];