本文介绍了Gson问题:-预期为BEGIN_OBJECT,但在第1行处为BEGIN_ARRAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Gson有点陌生,我有一个采用以下格式的json:-

I am bit new to Gson, I have a json in following format:-

{
  "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;

and have an java class:-package 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);

情况2:

List<SmtStatus> list = getGson().fromJson(result, STATUS_TYPE_Object );

我们非常感谢您的帮助.

Any help is really appreciated.

推荐答案

使用第二种情况,但替换

Use the second case, but replace

private SubTasks subTasks ;

private List<SubTasks> subTasks ;

线索出在错误中.

java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第276列为BEGIN_ARRAY 路径$ .subTasks

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 276 path $.subTasks

给定您的Java类,它希望有一个名为subTasks的对象,但却找到了一个数组.

Given your java classes, it was expecting an object named subTasks but found an array.

因此将其更改为数组,您就是黄金.

So change it to an array and you are gold.

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

The first case is probably correct, if you end up parsing an array of SMTStatus

这篇关于Gson问题:-预期为BEGIN_OBJECT,但在第1行处为BEGIN_ARRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 02:36