java - Deserialize JSON Data - Custom Deserialization Using Jackson JSON Parser -
i trying deserialize following incoming json data:
{"timetable":[{"personid":"100649771", ..,..,..,"xx":null}, {"personid":"100631701", ..,..,..,"xx":{"abc":1234,"xyz":5678}}], "xxx":"","xxxx":0,"xxxxx":false} but facing problem while parsing using custom deserialization block made of:
jparser.nexttoken(); while ((jparser.nexttoken() != jsontoken.end_array)) { string innerfield = jparser.getcurrentname(); jparser.nexttoken(); but in way skipping array contents while parsing second row in array (as illustrated in json sample above^).
update: here method(pastebin link) trying parse json data coming in described format. there way can bind json data directly bean? (imo appeared way more complex me because of json structure; can't change json structure nor bean structure. so, dropped idea of binding directly :| ) anyways here(pastebin link) bean well.
following sample of incoming json data:
{"schedules":[{"personid":"100649771", "hasschedule":false, "triptype":null, "stickernumber":null, "vehicleregno":null, "expectedstartdate":null, "actualstartdate":null, "actualenddate":null, "personscheduleddate":null, "shift":null, "coldcall":null, "pickuplocationcoord":null}, {"personid":"100631701", "hasschedule":true, "triptype":"p", "stickernumber":"pc0409", "vehicleregno":"asjhahsp1758", "expectedstartdate":"16 aug 2013, 10:00:00", "actualstartdate":"16 aug 2013, 10:02:52", "actualenddate":"16 aug 2013, 14:14:12", "personscheduleddate":null, "shift":"02:30 pm", "coldcall":"n", "pickuplocationcoord":{"latitude":92.01011101,"longitude":48.01011101}}], "errormessage":"","errorcode":0,"haserror":false} please can fireup pointers me here in order- deserialize 'em correctly? thanks
updated
among other things, mixing 2 aproaches read json stream: using readtree() json data in memory tree (like xml's dom) using jsonparser read json stream token token (like xml's jax). following method same using readtree(), seems more appropriate you reading json loaded in string:
public list<vehicleinformationbean> getallvehiclesintree(string response) { list<vehicleinformationbean> vehiclelist = new arraylist<vehicleinformationbean>(); try { personinformationbean mpersoninformationbean; databasehelper mdatabasehelper = databasehelper.getinstance(scontext); objectmapper mapper = new objectmapper(); objectnode root = (objectnode) mapper.readtree(response); if ((root.get(serviceconstant.errorcode).asint()) != 0 || !root.has(serviceconstant.schedules)) { return vehiclelist; } for(jsonnode element: root.get(serviceconstant.schedules)) { vehicleinformationbean lstvehicleinformation = new vehicleinformationbean(); if (element.has(serviceconstant.personid)) { string personid = element.get(serviceconstant.personid).astext(); mpersoninformationbean = mdatabasehelper.getpersondetailbyid(personid); lstvehicleinformation.setpersonid(personid); lstvehicleinformation.setname(mpersoninformationbean.getname()); lstvehicleinformation.setpickuplocation(mpersoninformationbean.getpickuplocation()); lstvehicleinformation.setdroplocation(mpersoninformationbean.getdroplocation()); } lstvehicleinformation.settriptype(element.get(serviceconstant.triptype).textvalue()); lstvehicleinformation.setstickernumber(element.get(serviceconstant.stickernumber).textvalue()); lstvehicleinformation.setvehicleregno(element.get(serviceconstant.vehicleregno).textvalue()); lstvehicleinformation.setexpectedstartdate(element.get(serviceconstant.expectedstartdate).textvalue()); lstvehicleinformation.setactualstartdate(element.get(serviceconstant.actualstartdate).textvalue()); lstvehicleinformation.setactualenddate(element.get(serviceconstant.actualenddate).textvalue()); lstvehicleinformation.setpersonscheduleddate(element.get(serviceconstant.personscheduleddate).textvalue()); lstvehicleinformation.setshift(element.get(serviceconstant.shift).textvalue()); if (element.has("pickuplocationcoord")) { jsonnode coords = element.get("pickuplocationcoord"); if(coords.has(serviceconstant.latitude)) { lstvehicleinformation.setlatitude(coords.get(serviceconstant.latitude).asdouble()); } if(coords.has(serviceconstant.longitude)) { lstvehicleinformation.setlongitude(coords.get(serviceconstant.longitude).asdouble()); } } else if (element.has(serviceconstant.coldcall)) { lstvehicleinformation.setcoldcall(element.get(serviceconstant.coldcall).textvalue()); } vehiclelist.add(lstvehicleinformation); } } catch (exception e) { // todo doing exception or throw if can't handled here e.printstacktrace(); } return vehiclelist; } there validations , code need add method exactly original method does. method shows main idea of how it.
Comments
Post a Comment