更改我的程序接受文件的方式将从 GSON 产生此错误:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $?

Changing the way my program accepts files will create this error from GSON: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $?

有很多关于这个话题的问题,但这个问题是不同的,因为我定义某个变量的方式决定了是否抛出这个错误。

基本上,我有以下 JSON 文件:

[
  {
    "appiumVersion":"1.8.1",
    "buildTag": "build-0",
    "newCommandTimeout": "30",
    "deviceName":"Samsung Galaxy S9 WQHD GoogleAPI Emulator",
    "deviceOrientation":"portrait",
    "browserName":"",
    "platformName":"Android",
    "platformVersion":"7.1",
    "app":"sauce-storage:ApiDemos-debug.apk"
  },
  {
    "appiumVersion":"1.8.1",
    "buildTag": "build-0",
    "newCommandTimeout": "30",
    "deviceName":"Samsung Galaxy S9 WQHD GoogleAPI Emulator",
    "deviceOrientation":"portrait",
    "browserName":"",
    "platformName":"Android",
    "platformVersion":"7.0",
    "app":"sauce-storage:ApiDemos-debug.apk"
  }
]

我正在使用 GSON 来解析它,这是它的代码:

File capsJsonFile = new File(capsFilePath);
Reader capsReader = null;

try {
    capsReader = new FileReader(capsJsonFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Gson gsonCapsJsonArr = new Gson();

JsonObject[] capsJsonArr = gsonCapsJsonArr.fromJson(capsReader,  JsonObject[].class);

然后,我有另一个程序调用上面的代码:

capsFile = "C:\Users\exue\IdeaProjects\j-mobile\tests\src\test\resources\capsJsonArray.json";
AutomationFramework.setCapsFilePath(capsFile);

并且此代码将 "capsFilePath" 变量设置为 "capsFile"。

奇怪的是,如果我这样写程序,我没有错误。但是,我想通过我正在使用的 maven-surefire-plugin 将这个 capsFile 字符串作为系统 属性 传递给 运行 这个程序,就像这样:

capsFile = System.getProperty("capsFile");
AutomationFramework.setCapsFilePath(capsFile);

我将插件配置为如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <configuration>
        <parallel>classes</parallel>
            <threadCount>4</threadCount>
        <redirectTestOutputToFile>false</redirectTestOutputToFile>
        <systemProperties>
            <property>
            <property>
                <name>capsFile</name>
                <value>C:\Users\exue\IdeaProjects\j-mobile\tests\src\test\resources\cfgJsonFile.json</value>
            </property>
        </systemProperties>
    </configuration>
</plugin>

这样写,把文件当作系统属性,现在报错:

[Utils] [ERROR] [Error] com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
        at com.google.gson.Gson.fromJson(Gson.java:939)
        at com.google.gson.Gson.fromJson(Gson.java:865)
        at com.transunion.qecop.automationframework.AutomationFramework.mobileDeviceDataProvider(AutomationFramework.java:208)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
        at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
        at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:927)
... 19 more

但如果我只是对字符串进行硬编码,则不会出现此错误。我输入文件的方式怎么可能导致这个错误?

出现此问题的原因是您指的是两个不同的 JSON

您传递的字符串是 capsJsonArray.json

而你在 maven 中传递的是 cfgJsonFile.json

我认为问题在于 json 被引用,因为它可能不正确