This question already has answers here:
Why does Gson fromJson throw a JsonSyntaxException: Expected BEGIN_OBJECT but was BEGIN_ARRAY?
(2个答案)
2年前关闭。
我对Gson有点陌生,我有以下格式的json:-
并有一个Java类:
包com.nab.testing.taf.config;
我正在这样解析:
情况1:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:
预期为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT
要么
情况2:
java.lang.IllegalStateException:应该为BEGIN_OBJECT,但之前为
第1行第276列的BEGIN_ARRAY路径$ .subTasks
任何帮助都非常感谢。
(2个答案)
2年前关闭。
我对Gson有点陌生,我有以下格式的json:-
{
"schedulerName" : "Commodities-ETP_Trade_Entry-FO_TCP_OAS_ALSWP-COM_SLS_BZ",
"startRequestDate" : "29-06-2017 23:39:54.910",
"activeTestCasesCount" : 7,
"statusMap" : { "Assigned" : 2, "In execution" : 1, "Pending" : 4 },
"subTaskCount" : 12,
"subTasks" : [
{ "testCaseName" : "OAS-TCP-ALSWP-0035", "testCaseType" : "DealEntry", "activeTestCase" : false, "statuses" : [ "Excluded" ], "currentStatus" : "Excluded", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0036", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending", "Assigned", "In execution" ], "currentStatus" : "In execution", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0037", "testCaseType" : "DealEntry", "activeTestCase" : false, "statuses" : [ "Excluded" ], "currentStatus" : "Excluded", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0039", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending", "Assigned" ], "currentStatus" : "Assigned", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0074", "testCaseType" : "DealEntry", "activeTestCase" : false, "statuses" : [ "Excluded" ], "currentStatus" : "Excluded", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0111", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending" ], "currentStatus" : "Pending", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0113", "testCaseType" : "DealEntry", "activeTestCase" : false, "statuses" : [ "Excluded" ], "currentStatus" : "Excluded", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0148", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending" ], "currentStatus" : "Pending", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0185", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending" ], "currentStatus" : "Pending", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0222", "testCaseType" : "DealEntry", "activeTestCase" : false, "statuses" : [ "Excluded" ], "currentStatus" : "Excluded", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0259", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending" ], "currentStatus" : "Pending", "message" : "" },
{ "testCaseName" : "OAS-TCP-ALSWP-0296", "testCaseType" : "DealEntry", "activeTestCase" : true, "statuses" : [ "Pending", "Assigned" ], "currentStatus" : "Assigned", "message" : "" }
],
"schedulerStatus" : "In execution",
"lastStatusDate" : "29-06-2017 23:40:19.251"}
并有一个Java类:
包com.nab.testing.taf.config;
import java.util.List;
/**
*
* Created by vpathani on 30/06/2017.
*/
public class SmtStatus {
public class SubTasks {
private String testCaseName;
private String testCaseType;
private boolean activeTestCase;
private String currentStatus;
public String getTestCaseName() {
return testCaseName;
}
public void setTestCaseName(String testCaseName) {
this.testCaseName = testCaseName;
}
public String getTestCaseType() {
return testCaseType;
}
public void setTestCaseType(String testCaseType) {
this.testCaseType = testCaseType;
}
public boolean isActiveTestCase() {
return activeTestCase;
}
public void setActiveTestCase(boolean activeTestCase) {
this.activeTestCase = activeTestCase;
}
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}
}
private String schedulerName;
private int activeTestCasesCount;
private int subTaskCount;
private SubTasks subTasks ;
private String schedulerStatus;
public String getSchedulerName() {
return schedulerName;
}
public void setSchedulerName(String schedulerName) {
this.schedulerName = schedulerName;
}
public int getActiveTestCasesCount() {
return activeTestCasesCount;
}
public void setActiveTestCasesCount(int activeTestCasesCount) {
this.activeTestCasesCount = activeTestCasesCount;
}
public int getSubTaskCount() {
return subTaskCount;
}
public void setSubTaskCount(int subTaskCount) {
this.subTaskCount = subTaskCount;
}
public String getSchedulerStatus() {
return schedulerStatus;
}
public void setSchedulerStatus(String schedulerStatus) {
this.schedulerStatus = schedulerStatus;
}
public SubTasks getSubTasks() {
return subTasks;
}
public void setSubTasks(SubTasks subTasks) {
this.subTasks = subTasks;
}
@Override
public String toString() {
return "SmtStatus{" +
"schedulerName='" + schedulerName + '\'' +
", activeTestCasesCount=" + activeTestCasesCount +
", subTaskCount=" + subTaskCount +
", subTasks=" + subTasks +
", schedulerStatus='" + schedulerStatus + '\'' +
'}';
}
}
我正在这样解析:
private static final Type STATUS_TYPE = new TypeToken<ArrayList<SmtStatus>>() { }.getType();
private static final Type STATUS_TYPE_Object = new TypeToken<SmtStatus>() { }.getType();
情况1:
List<SmtStatus> list = getGson().fromJson(result, STATUS_TYPE);
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:
预期为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT
要么
情况2:
List<SmtStatus> list = getGson().fromJson(result, STATUS_TYPE_Object );
java.lang.IllegalStateException:应该为BEGIN_OBJECT,但之前为
第1行第276列的BEGIN_ARRAY路径$ .subTasks
任何帮助都非常感谢。
最佳答案
使用第二种情况,但替换private SubTasks subTasks ;
与private List<SubTasks> subTasks ;
线索出在错误中。
java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在行1列276路径$ .subTasks中为BEGIN_ARRAY
给定您的Java类,它期待一个名为subTasks的对象,但找到一个数组。
因此,将其更改为数组,您就是金子。
如果您最终解析了一个SMTStatus数组,则第一种情况可能是正确的
关于java - Gson问题:-预期为BEGIN_OBJECT,但在第1行为BEGIN_ARRAY ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44837564/
10-08 21:26