如何使用Java在JSON路径上等于或不等于两个JSON对象

时间:2018-09-20 15:59:14

标签: java json geojson

想在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;
   }

1 个答案:

答案 0 :(得分:1)

如果结构一致,您可以做的一件事就是尝试通过将这些JSON对象映射到类来转换它们。如果发现不存在的任何其他键,它将引发异常,并且通过捕获它,您知道它们具有不同的结构。默认情况下,此功能是启用的,可以通过注释@JsonIgnoreProperties(ignoreUnknown = true)禁用它,然后使用它。

一种好的方法是添加Jackson库this is a good link tutorial并执行以下操作:

class Foo {
    @JsonProperty("squadName") String name;
    // (other props...) 
}