简体   繁体   中英

How to decode this nested JSON string?

i tried many times to decode nested JSON string which consist of nested Array , i used GSON() to do that. and it's worked , but the problem is about objects inside this array don't be decoded from json, so i want your help :

JSON String :

{ "data" : { "current_condition" : [ { "cloudcover" : "75",
            "humidity" : "87",
            "observation_time" : "03:16 AM",
            "precipMM" : "1.5",
            "pressure" : "991",
            "temp_C" : "11",
            "temp_F" : "52",
            "visibility" : "7",
            "weatherCode" : "293",
            "weatherDesc" : [ { "value" : "Patchy light rain" } ],
            "weatherIconUrl" : [ { "value" : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png" } ],
            "winddir16Point" : "SSW",
            "winddirDegree" : "210",
            "windspeedKmph" : "30",
            "windspeedMiles" : "19"
          } ],
      "request" : [ { "query" : "London, United Kingdom",
            "type" : "City"
          } ],
      "weather" : [ { "date" : "2014-01-03",
            "precipMM" : "6.9",
            "tempMaxC" : "10",
            "tempMaxF" : "50",
            "tempMinC" : "5",
            "tempMinF" : "41",
            "weatherCode" : "293",
            "weatherDesc" : [ { "value" : "Patchy light rain" } ],
            "weatherIconUrl" : [ { "value" : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png" } ],
            "winddir16Point" : "SW",
            "winddirDegree" : "220",
            "winddirection" : "SW",
            "windspeedKmph" : "33",
            "windspeedMiles" : "21"
          } ]
    } }

my Code to decode this JSON :

HashMap HashMap = new Gson().fromJson(json, HashMap.class);

and it's output :

{data={request=[{query=London, United Kingdom, type=City}], current_condition=[{cloudcover=75, humidity=87, observation_time=03:16 AM, precipMM=1.5, pressure=991, temp_C=11, temp_F=52, visibility=7, weatherCode=293, weatherDesc=[{value=Patchy light rain}], weatherIconUrl=[{value= http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png }], winddir16Point=SSW, winddirDegree=210, windspeedKmph=30, windspeedMiles=19}], weather=[{date=2014-01-03, precipMM=6.9, tempMaxC=10, tempMaxF=50, tempMinC=5, tempMinF=41, weatherCode=293, weatherDesc=[{value=Patchy light rain}], weatherIconUrl=[{value= http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png }], winddir16Point=SW, winddirDegree=220, winddirection=SW, windspeedKmph=33, windspeedMiles=21}]}}

so i want an ideal method to give me ability to enter this nested HashMap array and it's values , because my method don't give me ability to call nested values from this array

Thank you ,

由于该字符串代表另一个Hashmap中的Hashmap,因此您可以使用以下方法:

HashMap<String, HashMap> hashMap = new Gson().fromJson(json, HashMap.class);

It's worked after i did this method :

Type type = new TypeToken< HashMap<String, HashMap<String, ArrayList<com.google.gson.internal.LinkedTreeMap>>>>() {}.getType();

HashMap<String, HashMap<String, ArrayList<com.google.gson.internal.LinkedTreeMap>>> hashMap = new Gson().fromJson(API.getJSON(), type);    

com.google.gson.internal.LinkedTreeMap hash = hashMap.get("data").get("current_condition").get(0);

System.out.println("    "+hash.get("humidity")+"      ");

i know that your answers and my first method are true , but as i told you that i want to enter the entry nested array list inside this Json

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