1

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"}

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 + '\'' +
                '}';
    }
}

And i am parsing it like this:-

 private static final Type STATUS_TYPE = new TypeToken<ArrayList<SmtStatus>>() { }.getType();
 private static final Type STATUS_TYPE_Object = new TypeToken<SmtStatus>() { }.getType();

Case 1:

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

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

or

Case 2:

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

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

Any help is really appreciated.

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
Vikram Pathania
  • 873
  • 2
  • 8
  • 15

1 Answers1

5

Use the second case, but replace

private SubTasks subTasks ;

with

private List<SubTasks> subTasks ;

The clue was in the error.

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 276 path $.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.

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

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71