关于这个主题有很多问题,但这个问题是不同的,因为我定义某个变量的方式决定了是否抛出这个错误。
基本上,我有以下 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 被引用,因为它可能不正确
关于java - 更改我的程序接受文件的方式将会产生来自 GSON : Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $? 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52100577/