JSONObject和JSONArray的排序

运行环境:java1.8


方法:

/**
	 * 对jsonarray做比较:size不同,返回false; 排序后的String对比,不相同返回false;
	 * @param expect
	 * @param actual
	 * @return
	 */
	public static boolean compareJsonArray(JSONArray expect, JSONArray actual) {
		int expect_size = expect.size();
		int actual_size = actual.size();
		if (expect_size != actual_size) {
			logger.error("the two JSONArrays' size is not equal >>"
					+ "expect JSONArray's size  is [[" + expect.size()
					+ "]], but actual JSONArray's size is [[" + actual.size()
					+ "]]");
			return false;
		}
		if (!sortJsonArray(expect).toString().equals(
				sortJsonArray(actual).toString())) {
			logger.error("the two JSONArrays' value is not equal >>"
					+ "expect is \r\n" + expect + ", \r\nbut actual  is \r\n"
					+ actual);
			return false;
		}
		return true;
	}

	/**
	 * JSONObject排序
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("all")
	public static JSONObject sortJsonObject(JSONObject obj) {
		Map map = new TreeMap();
		Iterator it = obj.keys();
		while (it.hasNext()) {
			String key = it.next();
			Object value = obj.get(key);
			if (value instanceof JSONObject) {
				// System.out.println(value + " is JSONObject");
				map.put(key, sortJsonObject(JSONObject.fromObject(value)));
			} else if (value instanceof JSONArray) {
				// System.out.println(value + " is JSONArray");
				map.put(key, sortJsonArray(JSONArray.fromObject(value)));
			} else {
				map.put(key, value);
			}
		}
		return JSONObject.fromObject(map);
	}

	/**
	 * JSONArray排序
	 * 
	 * @param array
	 * @return
	 */
	@SuppressWarnings("all")
	public static JSONArray sortJsonArray(JSONArray array) {
		List list = new ArrayList();
		int size = array.size();
		for (int i = 0; i < size; i++) {
			Object obj = array.get(i);
			if (obj instanceof JSONObject) {
				list.add(sortJsonObject(JSONObject.fromObject(obj)));
			} else if (obj instanceof JSONArray) {
				list.add(sortJsonArray(JSONArray.fromObject(obj)));
			} else {
				list.add(obj);
			}
		}

		list.sort((o1, o2) -> o1.toString().compareTo(o2.toString()));
		return JSONArray.fromObject(list);
	}




测试:

String j1 = "{\"group\":[{\"group_id\":\"10408\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"last_uptime\":\"2015-05-20 10:39:16\",\"create_time\":\"2015-05-20 10:30:12\"},{\"group_id\":\"10414\",\"group_name\":\"\u95e8\u5e9798131\",\"last_uptime\":\"2015-05-20 10:40:04\",\"create_time\":\"2015-05-20 10:36:03\"}]}";
		String j2 = "{\"group\":[{\"group_id\":\"10408\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"last_uptime\":\"2015-05-20 10:39:16\",\"create_time\":\"2015-05-20 10:30:12\"},{\"group_id\":\"10414\",\"group_name\":\"\u95e8\u5e9798131\",\"last_uptime\":\"2015-05-20 10:40:04\",\"create_time\":\"2015-05-20 10:36:03\"}]}";
		String j3 = "{\"group\":[{\"group_id\":\"10414\",\"group_name\":\"\u95e8\u5e9798131\",\"last_uptime\":\"2015-05-20 10:40:04\",\"create_time\":\"2015-05-20 10:36:03\"}, {\"group_id\":\"10408\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"last_uptime\":\"2015-05-20 10:39:16\",\"create_time\":\"2015-05-20 10:30:12\"}]}";
		String j4 = "{\"group\":[{\"group_id\":\"10415\",\"group_name\":\"\u95e8\u5e9798131\",\"last_uptime\":\"2015-05-20 10:40:04\",\"create_time\":\"2015-05-20 10:36:03\"}, {\"group_id\":\"10408\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"last_uptime\":\"2015-05-20 10:39:16\",\"create_time\":\"2015-05-20 10:30:12\"}]}";
		String j5 = "{\"group\":[{\"group_id\":\"10408\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"last_uptime\":\"2015-05-20 10:39:16\",\"create_time\":\"2015-05-20 10:30:12\"}]}";
		String j6 = "{\"group\":[{\"create_time\":\"2015-05-20 10:30:12\",\"group_name\":\"\u4e94\u89d2\u573a\u9ec4\u5174\u5e97\",\"group_id\":\"10408\",\"last_uptime\":\"2015-05-20 10:39:16\"},{\"group_id\":\"10414\",\"last_uptime\":\"2015-05-20 10:40:04\",\"create_time\":\"2015-05-20 10:36:03\",\"group_name\":\"\u95e8\u5e9798131\"}]}";
		JSONObject o1 = JSONObject.fromObject(j1);
		JSONObject o2 = JSONObject.fromObject(j2);
		JSONObject o3 = JSONObject.fromObject(j3);
		JSONObject o4 = JSONObject.fromObject(j4);
		JSONObject o5 = JSONObject.fromObject(j5);
		JSONObject o6 = JSONObject.fromObject(j6);
		JSONArray a1 = o1.getJSONArray("group");
		JSONArray a2 = o2.getJSONArray("group");
		JSONArray a3 = o3.getJSONArray("group");
		JSONArray a4 = o4.getJSONArray("group");
		JSONArray a5 = o5.getJSONArray("group");
		JSONArray a6 = o6.getJSONArray("group");
		System.out.println("a1 和 a2比较: "
				+ CompareUtil.compareJsonArray(a1, a2));
		System.out.println("a1 和 a3比较: "
				+ CompareUtil.compareJsonArray(a1, a3));
		System.out.println("a1 和 a4比较: "
				+ CompareUtil.compareJsonArray(a1, a4));
		System.out.println("a1 和 a5比较: "
				+ CompareUtil.compareJsonArray(a1, a5));
		System.out.println("a1 和 a6比较: "
				+ CompareUtil.compareJsonArray(a1, a6));


你可能感兴趣的:(java,java工具类)