Parsing the nested JSON Array using Jackson library in java -
i know how parse following json using jackson library in java construct uri http://api.statdns.com/google.com/cname
{ "status": { "status": 200, "msg": "success" }, "apicalls": [ { "api": { "method": "get", "success": "200", "baseurl": "http://api.statdns.com/", "param1": "google.com/", "param2": "cname", "continue_on_fail": "1", "add_header2": "'accept', 'application/json'", "add_header1": "'content-type', 'application/json'", "client_id": "101" }, "id": 1385 } ] }
i have written bad code parse above json array. following code used,
public void parsejson(string json) { try{ jsonfactory factory = new jsonfactory(); jsonparser parser; parser = factory.createjsonparser(json); parser.setcodec(new objectmapper()); // avoid illegalstateexception jsontoken current; current = parser.nexttoken(); if (current != jsontoken.start_object) { system.out.println("error: root should object: quiting."); return; } while (parser.nexttoken() != jsontoken.end_object) { string fieldname = parser.getcurrentname(); // move field name field value current = parser.nexttoken(); if (fieldname.equals("apicalls")) { jsonnode node = parser.readvalueastree(); jsonnode currentjson = node.findvalue("api"); system.out.println("current json :: " + currentjson); jsonnode url = currentjson.get("baseurl"); jsonnode param1 = currentjson.get("param1"); jsonnode param2 = currentjson.get("param2"); string baseurl = url.astext(); string params1 = param1.astext(); string params2 = param2.astext(); string uri = baseurl + params1 + params2; system.out.println("url :: " + uri); initiaterestcall(uri); } } } catch (jsonparseexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } }
could me know parsing json using jackson? highly appreciated.
if using jackson library, should go this:
i using response http://api.statdns.com/google.com/cname
public void parsejson(string json) { jsonobject parse = new jsonobject(data); if(parse.get("question") instanceof jsonobject){ jsonobject questionjson = (jsonobject) parse.get("question"); system.out.println("name"+questionjson.getstring("name")); system.out.println("type"+questionjson.getstring("type")); system.out.println("class"+questionjson.getstring("class")); } else if(parse.get("question") instanceof jsonarray){ jsonarray questionjson = (jsonarray) parse.get("question"); string[] nameattrib=new string[questionjson.length()]; string[] typeattrib=new string[questionjson.length()]; string[] classattrib=new string[questionjson.length()]; for(int i=0;i<questionjson.length();i++){ jsonobject questionjsondata=(jsonobject)questionjson.get(i); nameattrib[i]=questionjsondata.getstring("name"); typeattrib[i]=questionjsondata.getstring("type"); classattrib[i]=questionjsondata.getstring("class"); system.out.println("name: "+nameattrib[i]); system.out.println("type: "+typeattrib[i]); system.out.println("class: "+classattrib[i]); } } else if (parse.get("question").equals(null)){ system.out.println("question"+null); } }
here doing "question" only, can other "answer", "authority" in case url have mentioned http://api.statdns.com/google.com/cname.
hopefully helps problem..!!!!
Comments
Post a Comment