【问题标题】:Gson issue:- Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 [duplicate]Gson 问题:- 预期 BEGIN_OBJECT 但在第 1 行是 BEGIN_ARRAY [重复]
【发布时间】:2017-12-03 21:19:44
【问题描述】:

我对 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();

案例一:

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 但为 BEGIN_ARRAY 在第 1 行第 276 列路径 $.subTasks

非常感谢任何帮助。

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    使用第二种情况,但替换

    private SubTasks subTasks ;

    private List&lt;SubTasks&gt; subTasks ;

    线索在错误中。

    java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 276 列是 BEGIN_ARRAY path $.subTasks

    鉴于您的 java 类,它期待一个名为 subTasks 的对象,但找到了一个数组。

    所以把它改成一个数组,你就是金子了。

    第一种情况可能是正确的,如果你最终解析一个 SMTStatus 数组

    【讨论】:

    • 感谢您的帮助
    • 如果有帮助,请考虑支持和接受 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2023-03-08
    • 2016-07-05
    • 1970-01-01
    • 2021-01-27
    相关资源
    最近更新 更多