簡體   English   中英

如何使用 getString() function 從 java 中的 JSON 獲取特定數據

[英]How do I fetch specific data from a JSON in java using getString() function

我正在嘗試從 JSON 文件中獲取天氣部分,但它顯示了一個異常。

這是我的代碼

JSONObject jsonObject = new JSONObject(result);
          String weather =  jsonObject.getString("weather");
           Log.i("Contents of URL", weather);
           //Log.i("Contents of URL", String.valueOf(jsonObject));  When I log this the whole JSON is printed to the console as expected.
        } catch (ExecutionException e) {
            System.out.println("EXCEPTION");
        } catch (InterruptedException e) {
            System.out.println("EXCEPTION");
        } catch (JSONException e) {
            System.out.println("EXCEPTION");
        }

這是從中提取此數據的 JSON 文件。

{"message":"","cod":"200","city_id":4887398,"calctime":0.0863,"cnt":4,"list":[{"main":{"temp":268.987,"temp_min":268.987,"temp_max":268.987,"pressure":1001.11,"sea_level":1024.68,"grnd_level":1001.11,"humidity":100},"wind":{"speed":5.06,"deg":291.002},"clouds":{"all":48},"**weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]**,"dt":1485703465},{"main":{"temp":268.097,"temp_min":268.097,"temp_max":268.097,"pressure":1003.57,"sea_level":1027.08,"grnd_level":1003.57,"humidity":100},"wind":{"speed":8.56,"deg":314.007},"clouds":{"all":44},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"dt":1485730032},{"main":{"temp":266.787,"temp_min":266.787,"temp_max":266.787,"pressure":1005.73,"sea_level":1029.63,"grnd_level":1005.73,"humidity":100},"wind":{"speed":6.79,"deg":316.012},"clouds":{"all":0},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"dt":1485755383},{"main":{"temp":263.64,"pressure":1015,"humidity":57,"temp_min":262.15,"temp_max":265.15},"wind":{"speed":2.6,"deg":280},"clouds":{"all":1},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"dt":1485780512}]}

我想要的是未打印的天氣信息。而不是打印異常之一。 如何僅從 JSON 獲取天氣信息?

您收到的 json 將天氣作為另一個鍵中的鍵:

{
  "message": "",
  "cod": "200",
  "city_id": 4887398,
  "calctime": 0.0863,
  "cnt": 4,
  "list": [
    {
      "main": {
        "temp": 268.987,
        "temp_min": 268.987,
        "temp_max": 268.987,
        "pressure": 1001.11,
        "sea_level": 1024.68,
        "grnd_level": 1001.11,
        "humidity": 100
      },
      "wind": {
        "speed": 5.06,
        "deg": 291.002
      },
      "clouds": {
        "all": 48
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "dt": 1485703465
    },
    {
      "main": {
        "temp": 268.097,
        "temp_min": 268.097,
        "temp_max": 268.097,
        "pressure": 1003.57,
        "sea_level": 1027.08,
        "grnd_level": 1003.57,
        "humidity": 100
      },
      "wind": {
        "speed": 8.56,
        "deg": 314.007
      },
      "clouds": {
        "all": 44
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "dt": 1485730032
    },
    {
      "main": {
        "temp": 266.787,
        "temp_min": 266.787,
        "temp_max": 266.787,
        "pressure": 1005.73,
        "sea_level": 1029.63,
        "grnd_level": 1005.73,
        "humidity": 100
      },
      "wind": {
        "speed": 6.79,
        "deg": 316.012
      },
      "clouds": {
        "all": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "Sky is Clear",
          "icon": "01n"
        }
      ],
      "dt": 1485755383
    },
    {
      "main": {
        "temp": 263.64,
        "pressure": 1015,
        "humidity": 57,
        "temp_min": 262.15,
        "temp_max": 265.15
      },
      "wind": {
        "speed": 2.6,
        "deg": 280
      },
      "clouds": {
        "all": 1
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "sky is clear",
          "icon": "01n"
        }
      ],
      "dt": 1485780512
    }
  ]
}

您可以清楚地看到您需要訪問列表鍵,然后訪問天氣鍵來獲取數據。

請注意,該列表是一個JSONArray

嘗試這個:

     String str = "{\n" + 
            "  \"message\": \"\",\n" + 
            "  \"cod\": \"200\",\n" + 
            "  \"city_id\": 4887398,\n" + 
            "  \"calctime\": 0.0863,\n" + 
            "  \"cnt\": 4,\n" + 
            "  \"list\": [\n" + 
            "    {\n" + 
            "      \"main\": {\n" + 
            "        \"temp\": 268.987,\n" + 
            "        \"temp_min\": 268.987,\n" + 
            "        \"temp_max\": 268.987,\n" + 
            "        \"pressure\": 1001.11,\n" + 
            "        \"sea_level\": 1024.68,\n" + 
            "        \"grnd_level\": 1001.11,\n" + 
            "        \"humidity\": 100\n" + 
            "      },\n" + 
            "      \"wind\": {\n" + 
            "        \"speed\": 5.06,\n" + 
            "        \"deg\": 291.002\n" + 
            "      },\n" + 
            "      \"clouds\": {\n" + 
            "        \"all\": 48\n" + 
            "      },\n" + 
            "      \"weather\": [\n" + 
            "        {\n" + 
            "          \"id\": 802,\n" + 
            "          \"main\": \"Clouds\",\n" + 
            "          \"description\": \"scattered clouds\",\n" + 
            "          \"icon\": \"03d\"\n" + 
            "        }\n" + 
            "      ],\n" + 
            "      \"dt\": 1485703465\n" + 
            "    },\n" + 
            "    {\n" + 
            "      \"main\": {\n" + 
            "        \"temp\": 268.097,\n" + 
            "        \"temp_min\": 268.097,\n" + 
            "        \"temp_max\": 268.097,\n" + 
            "        \"pressure\": 1003.57,\n" + 
            "        \"sea_level\": 1027.08,\n" + 
            "        \"grnd_level\": 1003.57,\n" + 
            "        \"humidity\": 100\n" + 
            "      },\n" + 
            "      \"wind\": {\n" + 
            "        \"speed\": 8.56,\n" + 
            "        \"deg\": 314.007\n" + 
            "      },\n" + 
            "      \"clouds\": {\n" + 
            "        \"all\": 44\n" + 
            "      },\n" + 
            "      \"weather\": [\n" + 
            "        {\n" + 
            "          \"id\": 802,\n" + 
            "          \"main\": \"Clouds\",\n" + 
            "          \"description\": \"scattered clouds\",\n" + 
            "          \"icon\": \"03d\"\n" + 
            "        }\n" + 
            "      ],\n" + 
            "      \"dt\": 1485730032\n" + 
            "    },\n" + 
            "    {\n" + 
            "      \"main\": {\n" + 
            "        \"temp\": 266.787,\n" + 
            "        \"temp_min\": 266.787,\n" + 
            "        \"temp_max\": 266.787,\n" + 
            "        \"pressure\": 1005.73,\n" + 
            "        \"sea_level\": 1029.63,\n" + 
            "        \"grnd_level\": 1005.73,\n" + 
            "        \"humidity\": 100\n" + 
            "      },\n" + 
            "      \"wind\": {\n" + 
            "        \"speed\": 6.79,\n" + 
            "        \"deg\": 316.012\n" + 
            "      },\n" + 
            "      \"clouds\": {\n" + 
            "        \"all\": 0\n" + 
            "      },\n" + 
            "      \"weather\": [\n" + 
            "        {\n" + 
            "          \"id\": 800,\n" + 
            "          \"main\": \"Clear\",\n" + 
            "          \"description\": \"Sky is Clear\",\n" + 
            "          \"icon\": \"01n\"\n" + 
            "        }\n" + 
            "      ],\n" + 
            "      \"dt\": 1485755383\n" + 
            "    },\n" + 
            "    {\n" + 
            "      \"main\": {\n" + 
            "        \"temp\": 263.64,\n" + 
            "        \"pressure\": 1015,\n" + 
            "        \"humidity\": 57,\n" + 
            "        \"temp_min\": 262.15,\n" + 
            "        \"temp_max\": 265.15\n" + 
            "      },\n" + 
            "      \"wind\": {\n" + 
            "        \"speed\": 2.6,\n" + 
            "        \"deg\": 280\n" + 
            "      },\n" + 
            "      \"clouds\": {\n" + 
            "        \"all\": 1\n" + 
            "      },\n" + 
            "      \"weather\": [\n" + 
            "        {\n" + 
            "          \"id\": 800,\n" + 
            "          \"main\": \"Clear\",\n" + 
            "          \"description\": \"sky is clear\",\n" + 
            "          \"icon\": \"01n\"\n" + 
            "        }\n" + 
            "      ],\n" + 
            "      \"dt\": 1485780512\n" + 
            "    }\n" + 
            "  ]\n" + 
            "}";
    
    JSONObject jsonObject = new JSONObject(str);        
    JSONArray listArray = jsonObject.getJSONArray("list");
    JSONObject weatherObj = new JSONObject();
    for (int i=0; i < listArray.length(); i++) {
        weatherObj = listArray.getJSONObject(i);
    }
    System.out.println(weatherObj.getString("weather"));
    
Output : [{"icon":"01n","description":"sky is clear","main":"Clear","id":800}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM