java - Best practice to receive JSON and convert it to an object -
i receiving json string in java, , wish convert object represents string. have function:
private arraylist<mydevice> parseresposne(string response) { arraylist<mydevice> devices = null; jsonarray jsnarr = null; try { // jsonobject jobj = new jsonobject(response); jsnarr = new jsonarray(response); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } (int = 0; < jsnarr.length(); i++) { mydevice tmpdevice = new mydevice(jsnarr.); // devices.add(tmpdevice); } return null; }
here mydevice class:
public class mydevice { public string name; public int deviceid; public string serialno; public string devicetype; public boolean enabled; public mydevice(int deviceid, string name, string serialno, string devicetype, boolean enabled) { this.deviceid = deviceid; this.name = name; this.serialno = serialno; this.devicetype = devicetype; this.enabled = enabled; } }
is there not easier way such model binder ins asp.net mvc?
what standard / best way convert json object?
you can use google's gson library easy conversion of json object , vice versa.
gson gson = new gson(); arraylist<mydevice> yourarray = gson.fromjson(jsonstring, new typetoken<list<mydevice>>(){}.gettype()); public class mydevice { public string name; public int deviceid; public string serialno; public string devicetype; public boolean enabled; //setters , getters }
Comments
Post a Comment