【问题标题】:Json Comparison Exception : Can't access getJSONArray in java [duplicate]Json比较异常:无法在java中访问getJSONArray [重复]
【发布时间】:2018-04-15 21:52:46
【问题描述】:
 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);
 }
}

我所期望的结果是,JSON1 下的 "id":"2" 或类似的东西中缺少密钥 Street,这提供了有助于我们识别缺少的密钥以执行 Junit 测试的正确信息。

我在 IDE 中遇到的错误是

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

请帮我解决这个问题。

【问题讨论】:

  • 你确定你是从正确的包中导入你的类吗?也许导入为“com.google.gson.JsonArray”应该像“org.json.something.JsonArray”
  • @StéphaneAmmar org.json.JSONArray 也是JSON Simple 中提供的一个类

标签: java json


【解决方案1】:

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

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

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

看看here to help more

【讨论】:

  • 这不是硬编码的值,这些是我必须给两个文件起的名字。
【解决方案2】:

如果您只想知道它们之间的区别,请使用以下代码(我将array2 更改为array1 用于比较它们的内容)。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多