代码之家  ›  专栏  ›  技术社区  ›  user2570135

如何将JSON解析为Dart列表

  •  0
  • user2570135  · 技术社区  · 7 年前

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

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

    这是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(数据体);

    0 回复  |  直到 7 年前
        1
  •  1
  •   mirkancal    7 年前

    您可以选择用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类表示。你也可以模拟位置和薪水。

        2
  •  0
  •   OldProgrammer    7 年前

      Map map = jsonDecode(data.body);
      List jobList = map["jobs"];
      for ( var job in jobList ) {
        print("id= ${job['id']} title=${job['title']}   location=${job['location']['city']} ");
      }