开发者

simplest way to parse JSON string

How can I navigate JSON string from one key to another nested key and get the value? I have the following string

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

So I answer my own question: In case someone else want to get value quickly: This is what I was looking for.

JSONObject j = new JSONObject(strResponse);

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


There are JSON libraries in pretty much any language. If you suggest one, I might be able to point you to something.

In the meantime, here are a few:

  • Perl JSON library
  • Python's is built in
  • Javascript serializes its objects in JSON so it's super easy
  • C# library -- also see C# - parsing json formatted data into nested hashtables
  • Here's the Android JSON reference

And so on. I suggest a quick google for the language of your choice.


Generally speaking you will use a library that is already built specifically for you language, what language are you trying to read the data in? Many languages have a couple of libraries available, some languages may have built in support, like JavaScript.

if you just need to understand the data, it is pretty readable...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜