【问题标题】:How do you parse JSON that is inside an array如何解析数组内的 JSON
【发布时间】:2021-09-25 13:30:29
【问题描述】:

我是 Flutter 开发的新手,正在尝试如何使用 Flutter HTTP 包 0.12.0+2。

如果响应看起来像这样......

{
    "coord": {
        "lon": -76.8403,
        "lat": 38.9649
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 93.47,
        "feels_like": 99.34,
        "temp_min": 88.84,
        "temp_max": 97.74,
        "pressure": 1017,
        "humidity": 46
    },
    "visibility": 10000,
    "wind": {
        "speed": 1.99,
        "deg": 304,
        "gust": 1.99
    },
    "clouds": {
        "all": 1
    },
    "dt": 1626462013,
    "sys": {
        "type": 2,
        "id": 2030617,
        "country": "US",
        "sunrise": 1626429293,
        "sunset": 1626481895
    },
    "timezone": -14400,
    "id": 4369076,
    "name": "Seabrook",
    "cod": 200
}

这是我的代码。而不是打印所有数据,我如何只在main内打印temp

void getData() async {

    Response response = await get('https://api.openweathermap.org/data/2.5/onecall?lat=38.964882&lon=-76.840271&exclude={part}&appid=b29e187fed23cf37dc160e6c115a270d');
    // print(response.body);
    Map data = jsonDecode(response.body);
    print(data);
  }

【问题讨论】:

    标签: flutter api android-studio http openweathermap


    【解决方案1】:

    你可以使用:

    void getData() async {
      Response response = await get('https://api.openweathermap.org/data/2.5/onecall?lat=38.964882&lon=-76.840271&exclude={part}&appid=b29e187fed23cf37dc160e6c115a270d');
      // print(response.body);
      Map data = jsonDecode(response.body);
      print(data['main']); //Will return [temp], [feels_like], [temp_min], etc..
      print(data['main']['temp']); //Will return [93.47]
    }
    

    【讨论】:

      【解决方案2】:

      您可以在解码后的 json 上使用运算符 [$field] 访问每个字段的值。像这样的:

      void getData() async {
      
       Response response = await get('https://api.openweathermap.org/data/2.5/onecall?lat=38.964882&lon=-76.840271&exclude=.{part}&appid=b29e187fed23cf37dc160e6c115a270d');
       // print(response.body);
       Map data = jsonDecode(response.body);
       print(data['main']); // prints out { temp: 93.47, ...., humidity: 46 }
       // what you want..
       final mainTemp = data['main']['temp'];
       print(mainTemp); // prints out 93.47.
       print(data);                                                            
      }
      

      所以,这就是您访问解码后的 json-string 响应的字段的方式。

      如果您打算在整个应用程序中使用这些接收到的值,请考虑将接收到的响应更改为接口,这将为您提供更大的灵活性并让您的代码看起来更简洁。

      【讨论】:

        【解决方案3】:

        在这里创建模型类你可以将json转换为dart https://javiercbk.github.io/json_to_dart/

        Future<YourmodelName>getData() async {
        YourmodelName data
        Response response = await get('https://api.openweathermap.org/data/2.5/onecall? 
        lat=38.964882&lon=-76.840271&exclude= 
         {part}&appid=b29e187fed23cf37dc160e6c115a270d');
        if (response.statusCode == 200) {
        data = YourmodelName.fromJson(response.data);;
        print(data.main.temp);}
        return data;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-14
          • 1970-01-01
          • 2015-06-13
          • 2015-09-06
          • 1970-01-01
          • 2021-07-28
          相关资源
          最近更新 更多