【问题标题】:Flutter - How to parse nested JSON structures with lists in flutter?Flutter - 如何解析带有列表的嵌套 JSON 结构?
【发布时间】:2020-10-29 05:28:19
【问题描述】:

这是我的 JSON

{
  "type-1":[
    {
      "name": "type-1-1"
    },
    {
      "name": "type-1-2"
    }
  ],
  "type-2": [
    {
      "name": "type-2-1"
    },
    {
      "name": "type-2-2"
    },
    {
      "name": "type-2-3"
    }
  ]
}

我想解析这个 JSON,所以我写了这样的模型

class Places {
  final List<PlaceName> names;
  Places({this.names});

  factory Places.fromJson(Map<String, dynamic> parsedJson){
    var list = parsedJson['name'] as List;
    List<PlaceName> placeList = list.map((i) => PlaceName.fromJson(i)).toList();
    return Places(
        names: placeList
    );
  }
}


class PlaceName {
  final String name;
  PlaceName({this.name});
  factory PlaceName.fromJson(Map<String, dynamic> parsedJson){
    return PlaceName(
        name : parsedJson['name'],
    );
  }
}

最后,我在小部件树中编写了这样的函数

Future<String> _loadAllPlaces() async {
    return await rootBundle.loadString('assets/location.json');
  }

  Future loadPlaces() async {
    String jsonString = await _loadAllPlaces();
    final jsonResponse = json.decode(jsonString);
    PlaceName place = new PlaceName.fromJson(jsonResponse);
    print(place);
    print(place.name);
    return place;
  }

所以当我在 FutureBuilder 中调用 loadPlaces() 时,它返回 null。我尝试调试它,但没有运气。请帮忙。提前致谢。

【问题讨论】:

  • 你试过什么调试?请分享。

标签: android json flutter


【解决方案1】:

在var list = parsedJson['name'] 行的Places.fromJson 方法中,您试图在引用对象本身之前访问对象内的列表。相反,您应该传递对象之一的名称,例如 type-1,这样var list = parsedJson['type-1'] as List;。

但这只会返回第一个对象的列表。要获取所有对象和每个对象的名称列表,您需要遍历 json 对象,在该示例中为 type-1 和 type-2,然后遍历其列表。您可以在AllPlaces.fromJson 中检查迭代。因此,您需要对代码进行一些更改以从 json 中获取所有对象。你可以试试下面的代码,你可以在DartPad进行测试

import 'dart:convert';

String jsonString = '{"type-1":[{"name": "type-1-1"},{"name": "type-1-2"}],"type-2": [{"name": "type-2-1"},{"name": "type-2-2"},{"name": "type-2-3"}]}';


class AllPlaces {
  List<PlaceName> placeNames;

  AllPlaces({this.placeNames});

  AllPlaces.fromJson(Map<String, dynamic> json) {
    placeNames = new List<PlaceName>();
    //Iterates over the json objects.
     json.forEach((type, list) {
      //Iterates over the list of each object.
      json[type].forEach((list) {
        //Creates an instance of PlaceName and adds to the placeNames list.
        placeNames.add(new PlaceName.fromJson(list));
      });
    });
  }
}

class PlaceName {
  String name;

  PlaceName({this.name});

  PlaceName.fromJson(Map<String, dynamic> json) {
    name = json['name'];
  }

}

Future loadPlaces() async {
   //String jsonString = await _loadAllPlaces();
   final jsonResponse = json.decode(jsonString);
   AllPlaces allPlaces = new AllPlaces.fromJson(jsonResponse);
  
   //Print the names of each object in the console
   allPlaces.placeNames.forEach((place) => print(place.name));
   
   return allPlaces;
}


void main() {
  loadPlaces();
}

【讨论】:

  • 请添加说明以阐明此代码与 OP 代码之间的差异。
  • @ChristopherMoore,我编辑了答案。检查它是否看起来更好。
猜你喜欢
  • 2020-05-21
  • 2021-02-25
  • 2019-01-24
  • 2015-01-15
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多