userJson {"CODE":{"ERROR_CODE":0},"DATA":[{"xm":"张三","id":10},{"xm":"李四","id":11},{"xm":"王五","id":12}]}
idJson {"CODE":{"ERROR_CODE":0},"DATA":[{"name":"aa","id":1},{"name":"bb","id":2},{"name":"cc","id":3}]}
在java中怎么抽取上述两个json中DATA部分,插入到一个json中。
形成{"user":[{"xm":"张三","id":10},{"xm":"李四","id":11},{"xm":"王五","id":12}],"id":[{"name":"aa","id":1},{"name":"bb","id":2},{"name":"cc","id":3}]}的形式
JSON Java
------解决方案--------------------
JSONObject jsonObject = new JSONObject();
JSONObject codeObject = new JSONObject();
codeObject.put("ERROR_CODE", 0);
JSONArray jsonArray = new JSONArray();
JSONObject jObject = new JSONObject();
jObject.put("xml","张三");
jObject.put("id", 10);
JSONObject jObject1 = new JSONObject();
jObject1.put("xml","李四");
jObject1.put("id", 11);
JSONObject jObject2 = new JSONObject();
jObject2.put("xml","王五");
jObject2.put("id", 12);
jsonArray.element(jObject).element(jObject1).element(jObject2);
jsonObject.put("CODE",codeObject);
jsonObject.put("DATA", jsonArray);
System.out.println(jsonObject);
JSONObject json = new JSONObject();
JSONObject code = new JSONObject();
code.put("ERROR_CODE", 0);
JSONArray array = new JSONArray();
JSONObject jo = new JSONObject();
jo.put("name","aa");
jo.put("id", 1);
JSONObject jo1 = new JSONObject();
jo1.put("name","bb");
jo1.put("id", 2);
JSONObject jo3 = new JSONObject();
jo3.put("name","cc");
jo3.put("id", 3);
array.element(jo).element(jo1).element(jo3);
json.put("CODE",code);
json.put("DATA", array);
System.out.println(json);
JSONObject object = new JSONObject();
JSONArray jsonArray2 = new JSONArray();
JSONArray a1 = jsonObject.getJSONArray("DATA");
ListIterator iterator = a1.listIterator();
while(iterator.hasNext()){
jsonArray2.element(iterator.next());
}