日期:2014-05-20  浏览次数:20668 次

在线急等!求JSON合并
本帖最后由 zmjfanfan 于 2013-06-19 10:24:44 编辑
classJSON    {"CODE":{"ERROR_CODE":0},"DATA":[{"bj":"初11级02班","classid":"2"}]}
classmateJSON    {"CODE":{"ERROR_CODE":0},"DATA":[{"xm":"张三","identityid":460},{"xm":"李四","identityid":462},{"xm":"王五","identityid":864},{"xm":"赵六","identityid":866}]}


怎么把classmateJSON的DATA部分加在classJSON的DATA里面,形成如下结果
{"CODE":{"ERROR_CODE":0},"DATA":[{"bj":"初11级02班","classid":"2","classmate":[{"xm":"张三","identityid":460},{"xm":"李四","identityid":462},{"xm":"王五","identityid":864},{"xm":"赵六","identityid":866}]}]}

在线等,急求!写明代码最好 谢谢!!
JSON java

------解决方案--------------------
	String a = "{'CODE':{'ERROR_CODE':0},'DATA':[{'bj':'初11级02班','classid':'2'}]}";
String b =  "{'CODE':{'ERROR_CODE':0},'DATA':[{'xm':'张三','identityid':460},{'xm':'李四','identityid':462},{'xm':'王五','identityid':864},{'xm':'赵六','identityid':866}]}";

JSONObject ja = JSONObject.fromObject(a); 
JSONObject jb = JSONObject.fromObject(b); 

ja.getJSONArray("DATA").getJSONObject(0).put("classmate",jb.get("DATA"));
System.out.println(ja.toString());