String json = "{\"shuxing\":\"dada\",\"list\":[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+ "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]}"; JSONObject jsonObject = JSONObject.fromObject( json ); System.out.println(jsonObject); // Dui bean = (Dui)JSONObject.toBean( jsonObject ); List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("list")); Map classMap = new HashMap(); classMap.put("list", AccountBean.class); Dui diyBean = (Dui)JSONObject.toBean(JSONObject.fromObject(json),Dui.class , classMap); System.out.println("....");
多重对象套嵌的
String json="[{\"list\":[{\"id\":1,\"nuId\":1,\"puId\":\"0000000000000001\",\"nuName\":\"lock\",\"timestamp\":0,\"basicType\":4,\"genericType\":64,\"specificType\":0,\"wakeup\":false,\"nodeInfo\":[]}],\"puId\":\"0000000000000001\"}]";
JSONArray jsonArray = JSONArray.fromObject(json);
DeviceList[] stus = (DeviceList[]) JSONArray.toArray(jsonArray, DeviceList.class);
for(int i=0;i<stus.length;i++)
{
DeviceList de_list=(DeviceList)stus[i];
List list=de_list.getList();
for(int j=0;j<list.size();j++)
{
Object obj=list.remove(i);
JSONObject jsonObject = JSONObject.fromObject(obj);
Device device = (Device) JSONObject.toBean( jsonObject,Device.class );
list.add(device);
}//重点为红色那句,因为好像没有 MorphDynaBean转java对象的例子,但是它是object啊
}