【发布时间】:2014-04-13 06:37:40
【问题描述】:
我在 java 中接收到一个 JSON 字符串,我希望将其转换为表示该字符串的对象。 我目前有这个功能:
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;
}
这是我的 Mydevice 类:
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;
}
}
有没有更简单的方法比如model binder ins asp.net mvc?
将 json 转换为对象的标准/最佳方法是什么?
【问题讨论】:
-
没有标准方法。您必须根据后端的响应解析 json
-
您应该查找 GSON。 code.google.com/p/google-gson
标签: java android json json.net