首页 > 解决方案 > 如何将 JSON 解析为 Dart 列表

问题描述

如何将复杂的 JSON 解析为 Darts/flutter 中的列表

我在地图中有消息,但我需要一些帮助来解析和获取值

这是 JSON:..

 {
        "jobs": [
            {
                "id": "S_1244",
                "title": "Title1",
                "location": {
                    "city": "Miami",
                    "stateCode": "FL"

                },
                "salary": {
                    "symbol": "US$",
                    "min": "26.15",
                    "max": "27.15"
                },
                "type": "Temporary",
                "posted": 1530027914570

            },

             {
                "id": "S_1234",
                "title": "Title1",
                "location": {
                    "city": "Miami",
                    "stateCode": "FL"

                },
                "salary": {
                    "symbol": "US$",
                    "min": "26.15",
                    "max": "27.15"
                },
                "type": "Temporary",
                "posted": 1530027914570

            }
       ]
 }

我的身体在地图上

地图地图 = jsonDecode(data.body);

谢谢你的帮助

标签: jsondartflutter

解决方案


您可以选择对 json 中的所有内容进行建模。这是开始的代码。

  Map myMap = json.decode(response.body);
  Iterable i = myMap['jobs'];
  List<Jobs> jobs = i.map((model) => Jobs.fromJson(model)).toList();
}

class Jobs {
  Jobs({this.id, this.title, this.type});
  String id;
  String title;
  String type;

  Jobs.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        title = json['title'],
        type = json['type'];
}

您将最终得到作业列表,这是您在 json 中的作业的普通旧 java 类表示。您也可以对位置和薪水进行建模。


推荐阅读