java - how to get json from inside a json -


i've below json data.

{   "restresponse" : {     "messages" : [ "more webservices available @ http://www.groupkt.com/post/f2129b88/services.htm", "total [249] records found." ],     "result" : [ {       "name" : "afghanistan",       "alpha2_code" : "af",       "alpha3_code" : "afg"     }, {       "name" : "Ă…land islands",       "alpha2_code" : "ax",       "alpha3_code" : "ala"     }, {       "name" : "albania",       "alpha2_code" : "al",       "alpha3_code" : "alb"     }, {       "name" : "algeria",       "alpha2_code" : "dz",       "alpha3_code" : "dza"     }]   } } 

here need loop through country names , print in console.

i'm trying below code.

objectmapper mapper = new objectmapper();             map<string, object> map = mapper.readvalue(text, new typereference<map<string, object>>() {             });              map<string, object> map1 = (map<string, object>) map.get("restresponse");             map<string, object> map2 = (map<string, object>) map1.get("result");                system.out.println(map1); 

when run exception

exception in thread "main" java.lang.classcastexception: java.util.arraylist cannot cast java.util.map     @ onlyjava.countries.getthecurrentweather(countries.java:45)     @ onlyjava.test.main(test.java:6) 

when comment out map<string, object> map2 = (map<string, object>) map1.get("result");, gives me result starting messages:.... till end.

please let me know going wrong , how can fix this.

updated code.

objectmapper mapper = new objectmapper();             map<string, object> map = mapper.readvalue(text, new typereference<map<string, object>>() {             });              map<string, object> map1 = (map<string, object>) map.get("restresponse");             list<object> resultlist = new arraylist<object>();             resultlist = mapper.readvalue(map1.get("result"), new typereference<list<object>>() {             });              system.out.println(resultlist); 

thanks

you getting jsonobject. value against key "restresponse" inside {} brackets again json object thats why able map map. value against key "result" inside [] brackets , list not json object can not assign map. should create arraylist assign that.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -