java - 如何使用java相对于json路径等于或不等于两个json对象
问题描述
想在json路径的帮助下比较两个json对象,两者的结构是否相同。我不考虑值,只考虑结构。提前感谢您的帮助例如下面两个json文件json-1和json-2,两者结构相同,但您可以看到值不同。
json-1
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "man1",
"age": 50,
"secretIdentity": "Dan",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
}
]
}
json-2
{
"squadName": "Super hero1",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Man2",
"age": 33,
"secretIdentity": "Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut1111",
"age": 30,
"secretIdentity": "ran",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
}
]
}
以下两个json具有不同的结构:
{
"squadName": "Super hero1",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Man2",
"age": 33,
"secretIdentity": "Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut1111",
"age": 30,
"secretIdentity": "ran",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
}
]
}
and
{
"Name": "Super hero1",
"Town": "Metro City",
"year": 2016,
"base": "Super tower",
"IsActive": true,
"associates": [
{
"name1": "Man2",
"age1": 33,
"secretIdentity1": "Jukes",
"powers1": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name1": "Madame Uppercut1111",
"age1": 30,
"secretIdentity1": "ran",
"powers1": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
}
]
我已经编写了如下所述的代码。从两个 json 对象中获取密钥并比较两者。想验证我的逻辑是否正确。
List<String>list4 =printJsonObject(obj);
List<String>list5 =printJsonObject(obj1);
if(list5.containsAll(list4)){
boolean res=true;
System.out.println("res"+res);
}
public static List printJsonObject(JSONObject jsonObj) throws
JSONException {
Iterator keys = jsonObj.keys();
List<String> jsonKeys=new ArrayList<String>();
while(keys.hasNext()){
//based on you key types
Object keyObj = (String)keys.next();
Object keyvalue = jsonObj.get(keyObj.toString());
jsonKeys.add(keyObj.toString());
//for nested objects iteration if required
if (keyvalue instanceof JSONObject)
printJsonObject((JSONObject)keyvalue);
}
return jsonKeys;
}
解决方案
如果结构一致,您可以做的一件事是尝试通过将这些 JSON 对象映射到类来转换它们。如果它发现任何其他不存在的键,它将引发异常,并且通过捕获它,您知道它们具有不同的结构。这是默认启用的,可以通过注释禁用@JsonIgnoreProperties(ignoreUnknown = true)
并使用它。
做到这一点的一个好方法是添加 Jackson 库,这是一个很好的链接教程并执行以下操作:
class Foo {
@JsonProperty("squadName") String name;
// (other props...)
}
推荐阅读
- html - 包装 div/样式表问题
- c++ - 如何在编译时将库(lib.a)链接到 cpp 文件?
- vue.js - vuejs 使用计算属性按日期排序
- corda - 在 Corda 中,两个节点如何相互证明他们都知道一条信息?
- python - Pygame重新调整游戏分辨率+坐标
- c# - EF Core 2.0 迁移在卫星程序集和用户机密中具有上下文
- sql - XQuery - 试图将 SQL 查询转换为 Xquery(分组、求和、内部连接)并且惨遭失败
- python - 确定许多时间序列之间时间协方差的方法?
- dynamics-crm - 导入错误解决方案:元素“实体”具有无效的子元素“IsRetrieveAuditEnabled”
- oracle-sqldeveloper - Oracle SQL Developer SQL 历史时间戳