Json比较例外:无法在java中访问getJSONArray

时间:2017-11-03 08:12:47

标签: java json

 JSON1:
 {
   "array1": [
    {
      "id": "1",
      "Name": "Xytrex Co.",
      "Description": "Industrial Cleaning Supply Company",
      "Account Number": "ABC15797531",
      "Address": {
       "Permanent": {
       "Street": "st.road",
       "pin": "789723"
       },
        "Temporary": {
         "Street": "st.road",
         "pin": "789723"
       }
     }
   },

{
  "id": "2",
  "Name": "Xytrex Co.",
  "Description": "Industrial Cleaning Supply Company",
  "Account Number": "ABC15797531",
  "Address": {
    "Permanent": {
      "Street": "st.road",
      "pin": "789723"
    },
    "Temporary": {
      "pin": "789723"
    }
  }
}
 ]
 }

JSON2:

     {
   "array2": [
     {
      "id": "1",
      "Name": "Xytrex Co.",
      "Description": "Industrial Cleaning Supply Company",
      "Account Number": "ABC15797531",
      "Address": {
        "Permanent": {
          "Street": "st.road",
          "pin": "789723"
        },
        "Temporary": {
          "Street": "st.road",
          "pin": "789723"
        }
      }
   },

  {
     "id": "2",
     "Name": "Xytrex Co.",
    "Description": "Industrial Cleaning Supply Company",
    "Account Number": "ABC15797531",
        "Address": {
        "Permanent": {
        "Street": "st.road",
        "pin": "789723"
       },
       "Temporary": {
        "Street": "st.road",
        "pin": "789723"
       }
     }
   }
 ]
 }

我使用的代码:

package com.company;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import com.google.gson.JsonParser;
import org.skyscreamer.jsonassert.JSONCompareMode;

import java.io.FileNotFoundException;
import java.io.FileReader;


public class Main {

public static void main(String[] args) throws FileNotFoundException {

    Main cmp = new Main();
    cmp.myTest();
}

public void myTest() throws FileNotFoundException {

     JsonParser Parser = new JsonParser();
     JsonObject obj1 = (JsonObject) Parser.parse(new 
 FileReader("/home/cloudera/Desktop/SampleJson/src/JSON1.json"));
    JsonArray array1 = obj1.getAsJsonArray("array1");
    JsonObject obj2 = (JsonObject)Parser.parse(new 
 FileReader("/home/cloudera/Desktop/SampleJson/src/JSON2.json"));
    JsonArray array2 = obj1.getAsJsonArray("array2");
    JSONAssert.assertEquals(array1, array2, JSONCompareMode.STRICT);
 }
}

我期待的结果是Street JSON1"id":"2"或类似的内容中缺少关键Junit,提供了有助于我们识别要执行的缺失密钥的正确信息{{ 1}}测试。

我在IDE中遇到的错误是

"java: cannot access org.json.JSONArray
  class file for org.json.JSONArray not found". 

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

我认为JSONArray对象类的初始化在这些行中似乎不正确。

jsonArray array1 = obj1.getAsJsonArray("array1");  // it may pron to the exception.
jsonArray array1 = obj1.getAsJsonArray("array1");

您尚未在方法getAsJsonArray()中传递正确的参数。您已传递了硬编码值"array1""array2",这不是正确的值,无法转换为JSONArray

看看here to help more

答案 1 :(得分:0)

如果您只是想知道它们之间的差异,请使用以下代码(我chagne array2array1来比较它们的内容)。

public class GsonClient {
public static void main(String[] args) throws Exception {
    GsonClient cmp = new GsonClient();
    cmp.myTest();
}

private void myTest() throws FileNotFoundException, JSONException {
    String json1Str = "{\n" +
            "   \"array1\": [\n" +
            "    {\n" +
            "      \"id\": \"1\",\n" +
            "      \"Name\": \"Xytrex Co.\",\n" +
            "      \"Description\": \"Industrial Cleaning Supply Company\",\n" +
            "      \"Account Number\": \"ABC15797531\",\n" +
            "      \"Address\": {\n" +
            "       \"Permanent\": {\n" +
            "       \"Street\": \"st.road\",\n" +
            "       \"pin\": \"789723\"\n" +
            "       },\n" +
            "        \"Temporary\": {\n" +
            "         \"Street\": \"st.road\",\n" +
            "         \"pin\": \"789723\"\n" +
            "       }\n" +
            "     }\n" +
            "   },\n" +
            "\n" +
            "{\n" +
            "  \"id\": \"2\",\n" +
            "  \"Name\": \"Xytrex Co.\",\n" +
            "  \"Description\": \"Industrial Cleaning Supply Company\",\n" +
            "  \"Account Number\": \"ABC15797531\",\n" +
            "  \"Address\": {\n" +
            "    \"Permanent\": {\n" +
            "      \"Street\": \"st.road\",\n" +
            "      \"pin\": \"789723\"\n" +
            "    },\n" +
            "    \"Temporary\": {\n" +
            "      \"pin\": \"789723\"\n" +
            "    }\n" +
            "  }\n" +
            "}\n" +
            " ]\n" +
            " }";
    String json2Str = "{\n" +
            "   \"array1\": [\n" +
            "     {\n" +
            "      \"id\": \"1\",\n" +
            "      \"Name\": \"Xytrex Co.\",\n" +
            "      \"Description\": \"Industrial Cleaning Supply Company\",\n" +
            "      \"Account Number\": \"ABC15797531\",\n" +
            "      \"Address\": {\n" +
            "        \"Permanent\": {\n" +
            "          \"Street\": \"st.road\",\n" +
            "          \"pin\": \"789723\"\n" +
            "        },\n" +
            "        \"Temporary\": {\n" +
            "          \"Street\": \"st.road\",\n" +
            "          \"pin\": \"789723\"\n" +
            "        }\n" +
            "      }\n" +
            "   },\n" +
            "\n" +
            "  {\n" +
            "     \"id\": \"2\",\n" +
            "     \"Name\": \"Xytrex Co.\",\n" +
            "    \"Description\": \"Industrial Cleaning Supply Company\",\n" +
            "    \"Account Number\": \"ABC15797531\",\n" +
            "        \"Address\": {\n" +
            "        \"Permanent\": {\n" +
            "        \"Street\": \"st.road\",\n" +
            "        \"pin\": \"789723\"\n" +
            "       },\n" +
            "       \"Temporary\": {\n" +
            "        \"Street\": \"st.road\",\n" +
            "        \"pin\": \"789723\"\n" +
            "       }\n" +
            "     }\n" +
            "   }\n" +
            " ]\n" +
            " }";
    JSONAssert.assertEquals(json1Str, json2Str, true);
    }
}

pom文件如下所示。

<dependency>
    <groupId>org.skyscreamer</groupId>
    <artifactId>jsonassert</artifactId>
    <version>1.5.0</version>
</dependency>

,输出在这里:

Exception in thread "main" java.lang.AssertionError: array1[1].Address.Temporary
Unexpected: Street

    at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:417)
    at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:394)
    at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:336)
    at com.henryxi.gson.GsonClient.myTest(GsonClient.java:89)
    at com.henryxi.gson.GsonClient.main(GsonClient.java:12)