比较JSON嵌套数组和jsons(阵列顺序无所谓) [英] Compare JSON with nested arrays and jsons (Array order does not matter)

查看:236
本文介绍了比较JSON嵌套数组和jsons(阵列顺序无所谓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在java中比较两个JSON,每个按键可以包含一个JSON对象,或JSON对象数组,和他们每个人也可以是一个数组或JSON。

Hello I'm trying to compare two json in java, each key can contain a json object or array of json objects, and each one of them can also be an array or json.

下面是JSON的一个例子:

Here is an example of the Json:

{
  "id": "123123asd123",
  "attributes": [
      {
         "name": "apps",
         "values": [
             "111",
             "222"
             ]
      },
      {
         "name": "city",
         "values": [
              "NY"
              ]
      }
    ]
}

我希望能够从这种得到两个JSON和无需关心阵列的顺序进行比较。
正如你所看到的钥匙属性是JSON数组所以如果我有这样的一起来这里和重点城市的数组中的元素另一个JSON是之前的应用程序,我想测试通过。
的应用价值我不里面的数字藏汉关心,如果它是111222或222111

I want to be able to get two json from this kind and compare them without caring about the order of the arrays. As you can see the key 'attributes is an array of json so if i have another json like the one up here and the element in the array with key city is before apps i want the test to pass. aswell of the numbers inside the apps values i dont care if it is 111,222 or 222,111

如果有谁知道是干什么的病很高兴听到任何外部Java库。
或任何想法如何实现这一手动比较?甚至一个想法,将这种JSON和重组它,所以它会很容易比较的份额与我。

If anyone know any external java lib that is doing that ill be happy hear. Or any idea how to implement this compare manually? or even an idea that will this kind of json and reorganize it so it will be easy to compare share that with me.

推荐答案

看看这个库她是酷我使用这一切的一天我的web服务测试:

Take a look at this library she is cool i'm using it all the day for my Webservice Test:

https://github.com/jayway/JsonPath

@Test
public void test() {
    String json = "{\n" +
            "  \"id\": \"123123asd123\",\n" +
            "  \"attributes\": [\n" +
            "      {\n" +
            "         \"name\": \"apps\",\n" +
            "         \"values\": [\n" +
            "             \"111\",\n" +
            "             \"222\"\n" +
            "             ]\n" +
            "      },\n" +
            "      {\n" +
            "         \"name\": \"city\",\n" +
            "         \"values\": [\n" +
            "              \"NY\"\n" +
            "              ]\n" +
            "      }\n" +
            "    ]\n" +
            "}";

    List<String> names = JsonPath.read(json, "$.attributes[*].name");
    for(String name : names) {
        //TODO assert that name is in other list from other json
    }

这篇关于比较JSON嵌套数组和jsons(阵列顺序无所谓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆