I am receiving a JSON string in java, and I wish to convert it to an object that represents the string. I currently have this 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();
}
for (int i = 0; i < jsnArr.length(); i++) {
MyDevice tmpDevice = new MyDevice(jsnArr.);
// devices.add(tmpDevice);
}
return null;
}
here is my 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 an easier way such as the model binder ins asp.net mvc?
What is the standard / best way to convert json to an object?