简体   繁体   中英

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:

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM