java - On parsing gson.toJson(obj) giving null -
wwhen passing object of local-inner-class shipaddress
tojson()
method of gson
class returning null on parsing it.
public class crusialdaterest { public string getshippingaddressesdetails() { gson gson = new gsonbuilder().create(); try { collection<impladdress> savedaddressbeans = new arraylist<impladdress>(); collection<ctflexfield> countryfields = new arraylist<ctflexfield>(); collection<ctflexfield> debitorfields = new arraylist<ctflexfield>(); class shipaddress{ collection<impladdress> savedaddressbean = new arraylist<impladdress>(); collection<ctflexfield> countryfield = new arraylist<ctflexfield>(); collection<ctflexfield> debitorfield = new arraylist<ctflexfield>(); shipaddress( collection<impladdress> savedaddressbeans, collection<ctflexfield> countryfields,collection<ctflexfield> debitorfields){ savedaddressbean=savedaddressbeans; countryfield=countryfields; debitorfield=debitorfields; } } string addrid= xmlparser.getnodevalue(address, statics.buyflow_namespace, "addressid"); string addrstreet1 = xmlparser.getnodevalue(address, statics.buyflow_namespace, "addrstreet1"); string addrstreet2 = xmlparser.getnodevalue(address, statics.buyflow_namespace, "addrstreet2"); string addrstreet3 = xmlparser.getnodevalue(address, statics.buyflow_namespace, "addrstreet3"); impladdress impladdress = new impladdress(); impladdress.setaddressid(addrid); impladdress.setaddrstreet1(addrstreet1); impladdress.setaddrstreet2(addrstreet2); impladdress.setaddrstreet3(addrstreet3); savedaddressbeans.add(impladdress); } ctflexfield[] flexfield = flexfields.getflexfield(); (ctflexfield flex : flexfield) { if(flex.getbundle().equalsignorecase("countries")){ countryfields.add(flex); } else if(flex.getbundle().equalsignorecase("commonbundle")){ debitorfields.add(flex); } } jsonresponse = gson.tojson(new shipaddress(savedaddressbeans,countryfields,debitorfields)); out.debug("jsonresponse--"+jsonresponse); } catch (exception e) { out.error("rest method getshippingaddresses error", e); } return jsonresponse; } }
should make inner class outside method?
or serialization issue?
it's visibility issue.
either move shipaddress
own class file, or make public static
inner class.
note public static classes cannot declared inside of methods, have move class out of getshippingaddressdetails()
method.
Comments
Post a Comment