网站建设及管理制度,dedecms做企业网站,大型网站故障,电子商务网站建设论文摘要在本例中java解析JSON对象使用的是org.json#xff0c;因此#xff0c;如果各位想测试我的代码#xff0c;请先确保有java.json.jar包#xff0c;否则#xff0c;就需要去网上下载这个jar包#xff0c;然后才可以正常使用本代码。本例的功能就是对两个json对象#xff0…在本例中java解析JSON对象使用的是org.json因此如果各位想测试我的代码请先确保有java.json.jar包否则就需要去网上下载这个jar包然后才可以正常使用本代码。本例的功能就是对两个json对象将一个json对象合并到另一个json对象中。注意这两个json对象中的key是相同的。其中被追加的json对象如下{className: User,condition: [{pro: frontNum,connector: in,values: [1,2,3,4]},{pro: sex,connector: !,values: [男,女]}]}要合并到上面的json对象中的json为[{pro: frontNum,connector: in,values: [1,2]},{pro: sex,connector: !,values: [人妖]},{pro: name,connector: !,values: [曹操,刘备,章鱼,张飞,马超,孙武]},{pro: home,connector: !,values: [山东,北京,河北,安徽,广东,福建]}]注意上述两个json字符串仅用于朋友们便于查看它们的结构测试代码时不需要拷贝上面的代码。下面是处理上述实现要求的代码。package view;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class TestUtil {public static void main(String[] args) throws JSONException {String endString {\className\: \User\,\condition\: [{\pro\: \frontNum\,\connector\: \in\,\values\: [\1\,\2\,\3\,\4\]},{\pro\: \sex\,\connector\: \!\,\values\: [\男\,\女\]}]};JSONObject jsonObject new JSONObject(endString);String comString [{\pro\: \frontNum\,\connector\: \in\,\values\: [\1\,\2\]},{\pro\: \sex\,\connector\: \!\,\values\: [\人妖\]},{\pro\: \name\,\connector\: \!\,\values\: [\曹操\,\刘备\,\章鱼\,\张飞\,\马超\,\孙武\]},{\pro\: \home\,\connector\: \!\,\values\: [\山东\,\北京\,\河北\,\安徽\,\广东\,\福建\]}];JSONArray jsonArray new JSONArray(comString);JSONArray jsonArray2 jsonObject.getJSONArray(condition);for (int i 0; i JSONObject jsonObject2 jsonArray.getJSONObject(i);jsonArray2 updateCondition(jsonArray2, jsonObject2);}System.out.println(jsonArray2.toString());}/*** 在类名已经匹配完成的前提下将从规则类中 取到一个包含列明连接符值的对象追加进入JSONarray** param jsonArray* 某个类名下的condition中的数组被追加的约束的对象* param comOb* 要追加的对象* return* throws JSONException*/public static JSONArray updateCondition(JSONArray jsonArray,JSONObject comOb) throws JSONException {JSONArray endJsonArray new JSONArray();String comPro1 comOb.getString(pro);String comConn1 comOb.getString(connector);boolean flag true;for (int i 0; i JSONObject enJsOb1 jsonArray.getJSONObject(i);String enPro1 enJsOb1.getString(pro);String enConn1 enJsOb1.getString(connector);// 属性名和连接符都相同则在这个json中添加进来if (enPro1.trim().equals(comPro1.trim()) enConn1.trim().equals(comConn1.trim())) {flag false;JSONArray comJsAr1 comOb.getJSONArray(values);for (int m 0; m String comVal comJsAr1.getString(m);JSONArray enJsAr1 enJsOb1.getJSONArray(values);JSONArray tempJsonArray comPareVal(comVal, enJsAr1);enJsOb1.put(values, tempJsonArray);}endJsonArray.put(enJsOb1);continue;}endJsonArray.put(jsonArray.get(i));}if (true flag) {endJsonArray.put(comOb);}return endJsonArray;}/*** 将一个值追加进一个json数组中并判断数组中 是否已经存在这个值如果存在则不追加** param comVal* 要追加的值* param enJsonArray* 被追加的数组* return 追加完成后的jso数组* throws JSONException*/public static JSONArray comPareVal(String comVal, JSONArray enJsonArray)throws JSONException {boolean flag true;for (int i 0; i String enVal enJsonArray.getString(i);if (!comVal.trim().equals(enVal.trim())) {flag false;} else {flag true;break;}}if (true flag) {return enJsonArray;} else {enJsonArray.put(comVal);return enJsonArray;}}}经过上述的代码我们就可以将这两个json对象合并为同一个对象{condition: [{values: [1,2,3,4],pro: frontNum,connector: in},{values: [男,女,人妖],pro: sex,connector: !},{values: [曹操,刘备,章鱼,张飞,马超,孙武],pro: name,connector: !},{values: [山东,北京,河北,安徽,广东,福建],pro: home,connector: !}],className: User}OK,完美实现合并JSON对象的功能。