gpt4 book ai didi

java - 使用java进行语义json数组比较

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:34 25 4
gpt4 key购买 nike

是否有内置库或代码可以帮助我在语义上比较两个 json 数组:

我正在运行单元测试,必须比较两个 json 数组并查看它们是否相等

Json 数组1=

[{"job_name":"CE","role_name":"Excellence"},
{"job_name":"PA","role_name":"Approver"},
{"job_name":"RO","role_name":"Financial"},
{"job_name":"RP","role_name":"Publisher"},
{"job_name":"SA","role_name":"Analyst"},
{"job_name":"TS","role_name":"Supervisor"},
{"job_name":"TT","role_name":"test"},
{"job_name":"ZG","role_name":"Manager"},
{"job_name":"ZI","role_name":"Manager"},
{"job_name":"ZJ","role_name":"Manager"}]

Json 数组2:

{"role_name":"Excellence","job_name":"CE"},
{"role_name":"Approver","job_name":"PA"},
{"role_name":"Financial","job_name":"RO"},
{"role_name":"Publisher","job_name":"RP"},
{"role_name":"Analyst","job_name":"SA"},
{"role_name":"Supervisor","job_name":"TS"},
{"role_name":"test","job_name":"TT"},
{"role_name":"Manager","job_name":"ZG"},
{"role_name":"Manager","job_name":"ZI"},
{"role_name":"Manager","job_name":"ZJ"}

当我使用 http://www.jsondiff.com/ 进行比较时,它返回两个文件在语义上相同。

对于 Java 代码,我尝试使用以下两个库:

JSONCompareResult result = JSONCompare.compareJSON(Array1, Array1, 
JSONCompareMode.STRICT) :

JSONCompareResult result = JSONCompare.compareJSON(Array1, Array1,
JSONCompareMode.LENIENT) :

此外,我尝试使用 JSON 断言: JSONAssert.assertEquals(Array1, Array1, false);

但在这两种情况下,库都会将 JSON 数组标记为不同。

任何帮助或建议将不胜感激。

最佳答案

对于你想要的东西来说,这可能很啰嗦,我不知道任何捷径,所以我倾向于尝试解决它。您需要检查数组的长度是否相等,顺序是否相同等等......但有时它可能会对某人有所帮助..

import org.json.JSONArray;
import org.json.JSONObject;

public class ClassCompareTwoJsonArrays {

private static String JSONArray1 = "[{\"job_name\":\"CE\",\"role_name\":\"Excellence\"},"
+ "{\"job_name\":\"PA\",\"role_name\":\"Approver\"},"
+ "{\"job_name\":\"RO\",\"role_name\":\"Financial\"},"
+ "{\"job_name\":\"RP\",\"role_name\":\"Publisher\"},"
+ "{\"job_name\":\"SA\",\"role_name\":\"Analyst\"},"
+ "{\"job_name\":\"TS\",\"role_name\":\"Supervisor\"},"
+ "{\"job_name\":\"TT\",\"role_name\":\"Test1\"},"
+ "{\"job_name\":\"ZG\",\"role_name\":\"Manager\"},"
+ "{\"job_name\":\"ZI\",\"role_name\":\"Manager\"},"
+ "{\"job_name\":\"ZJ\",\"role_name\":\"Manager\"}]";

private static String JSONArray2 = "{\"role_name\":\"Excellence\",\"job_name\":\"CE\"},"
+ "{\"role_name\":\"Approver\",\"job_name\":\"PA\"},"
+ "{\"role_name\":\"Financial\",\"job_name\":\"RO\"},"
+ "{\"role_name\":\"Publisher\",\"job_name\":\"RP\"},"
+ "{\"role_name\":\"Analyst\",\"job_name\":\"SA\"},"
+ "{\"role_name\":\"Supervisor\",\"job_name\":\"TS\"},"
+ "{\"role_name\":\"Test1\",\"job_name\":\"TT\"},"
+ "{\"role_name\":\"Manager\",\"job_name\":\"ZG\"},"
+ "{\"role_name\":\"Manager\",\"job_name\":\"ZI\"},"
+ "{\"role_name\":\"Manager\",\"job_name\":\"ZJ\"}";


public static void main(String[] args) {

//Array1 is set out like an array enclosed in []
//Array2 is not so...
JSONArray2 = "[" +JSONArray2 +"]";


//Modify Array1 to allow JSON to read array by giving the array a name
//Possibly an unneccesary work around - Self taught...
JSONArray1 = "{Test1:{JSONArray1:" +JSONArray1 +"}}";
JSONObject objAll = new JSONObject(JSONArray1);
JSONObject objTest1 = objAll.getJSONObject("Test1");
JSONArray arrJSONArray1 = objTest1.getJSONArray("JSONArray1");


//Modify Array1 to allow JSON to read array by giving the array a name
//Possibly an unneccesary work around - Self taught...
JSONArray2 = "{Test2:{JSONArray2:" +JSONArray2 +"}}";
JSONObject objAll2 = new JSONObject(JSONArray2);
JSONObject objTest2 = objAll2.getJSONObject("Test2");
JSONArray arrJSONArray2 = objTest2.getJSONArray("JSONArray2");


// Initilise counts for record check results
int FoundTheSame = 0;
int FoundDifferent = 0;


//Read Arrays and compare - code assumes arrays are the same length and order of records
for (int i = 0; i < arrJSONArray1.length(); ++i)
{

//Read data from Array1 for comparison
JSONObject objTestJSONArray1 = arrJSONArray1.getJSONObject(i);
String JSONArray1Job = objTestJSONArray1.getString("job_name");
String JSONArray1Role = objTestJSONArray1.getString("role_name");

//Read data from Array2 for comparison
JSONObject objTestJSONArray2 = arrJSONArray2.getJSONObject(i);
String JSONArray2Job = objTestJSONArray2.getString("job_name");
String JSONArray2Role = objTestJSONArray2.getString("role_name");


//Compare arrays and show results
if (JSONArray1Job.equals(JSONArray2Job) && JSONArray1Role.equals(JSONArray2Role))
{ // The same
FoundTheSame = FoundTheSame + 1;
} else {
//Different
FoundDifferent = FoundDifferent + 1;
System.out.println("Error" + " " +FoundDifferent +", "
+"Array Index (starts at 0) = " +i +", Array1 = " +JSONArray1Job +" "
+ JSONArray1Role +", Array2 = " +JSONArray2Job +", " + JSONArray2Role);

}//end if
}//end for

System.out.println("Records found to be the same = " +FoundTheSame + " of " +arrJSONArray1.length());

}//end main
}//end class

当它们与您提供的数据相同时,会产生以下结果......

Records found to be the same = 10 of 10

或者,如果我更改了某些内容以突出显示错误,则示例响应是...

Error 1, Array Index (starts at 0) = 4, Array1 = SA Analyst, Array2 = SA, Analystt
Error 2, Array Index (starts at 0) = 8, Array1 = AA Manager, Array2 = ZI, Manager
Records found to be the same = 8 of 10

关于java - 使用java进行语义json数组比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49311229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com