解析JSON字符串的最简单方法

时间:2011-05-09 21:32:43

标签: java android json

如何将JSON字符串从一个键导航到另一个嵌套键并获取值?我有以下字符串

{ "data" : { "current_condition" : [ { "cloudcover" : "75",
            "humidity" : "29",
            "observation_time" : "07:59 PM",
            "precipMM" : "0.0",
            "pressure" : "1011",
            "temp_C" : "19",
            "temp_F" : "67",
            "visibility" : "16",
            "weatherCode" : "116",
            "weatherDesc" : [ { "value" : "Partly Cloudy" } ],
            "weatherIconUrl" : [ { "value" : "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" } ],
            "winddir16Point" : "N",
            "winddirDegree" : "350",
            "windspeedKmph" : "26",
            "windspeedMiles" : "16"
          } ],
      "request" : [ { "query" : "01801",
            "type" : "Zipcode"
          } ],
      "weather" : [ { "date" : "2011-05-09",
            "precipMM" : "0.0",
            "tempMaxC" : "19",
            "tempMaxF" : "65",
            "tempMinC" : "10",
            "tempMinF" : "50",
            "weatherCode" : "113",
            "weatherDesc" : [ { "value" : "Sunny" } ],
            "weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0001_sunny.png" } ],
            "winddir16Point" : "NNW",
            "winddirDegree" : "348",
            "winddirection" : "NNW",
            "windspeedKmph" : "24",
            "windspeedMiles" : "15"
          },
          { "date" : "2011-05-10",
            "precipMM" : "0.1",
            "tempMaxC" : "13",
            "tempMaxF" : "56",
            "tempMinC" : "12",
            "tempMinF" : "53",
            "weatherCode" : "122",
            "weatherDesc" : [ { "value" : "Overcast" } ],
            "weatherIconUrl" : [ { "value" : "http://www/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png" } ],
            "winddir16Point" : "NNE",
            "winddirDegree" : "12",
            "winddirection" : "NNE",
            "windspeedKmph" : "31",
            "windspeedMiles" : "19"
          }
        ]
    } }

所以我回答我自己的问题: 如果其他人想要快速获得价值:这就是我想要的。

JSONObject j = new JSONObject(strResponse);

String weatherDesc = jObject.getJSONObject("data").getJSONArray("weather").getJSONObject(0).getJSONAr­ray("weatherDesc").getJSONObject(0).getString("value");

2 个答案:

答案 0 :(得分:3)

几乎所有语言都有JSON库。如果你建议一个,我可能会指出你的事情。

与此同时,以下是一些:

等等。我建议快速谷歌选择您的语言。

答案 1 :(得分:1)

一般来说,您将使用专门为您的语言构建的库,您尝试读取数据的语言是什么?许多语言都有一些库可用,某些语言可能内置支持,如JavaScript。

如果您只是需要了解数据,那么它非常易读......