首页 > 解决方案 > 如何使用 getString() 函数从 Java 中的 JSON 中获取特定数据

问题描述

我正在尝试从 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 中只获取天气信息?

标签: javaandroidjson

解决方案


尝试这个 :

     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}]

推荐阅读