`
envy2002
  • 浏览: 153935 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

json转java对象

 
阅读更多

 

		 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啊
			
		 }
		 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics