sanket Thakare sanket Thakare - 1 month ago 15
JSON Question

Trying to parse JSON data using GSON in android: Expected BEGIN_ARRAY but was BEGIN_OBJECT

I am trying to parse data using GSON in android but getting error :


java.lang.IllegalStateException: Expected BEGIN_ARRAY but was
BEGIN_OBJECT at line 1 column 2.


How can i parse this data to stored in Model class.

JSON data:

{"activities-steps":[{"dateTime":"2016-09-24","value":"0"},{"dateTime":"2016-09-25","value":"0"},{"dateTime":"2016-09-26","value":"0"},{"dateTime":"2016-09-27","value":"0"},{"dateTime":"2016-09-28","value":"0"},{"dateTime":"2016-09-29","value":"0"},{"dateTime":"2016-09-30","value":"0"},{"dateTime":"2016-10-01","value":"0"},{"dateTime":"2016-10-02","value":"0"},{"dateTime":"2016-10-03","value":"0"},{"dateTime":"2016-10-04","value":"0"},{"dateTime":"2016-10-05","value":"0"},{"dateTime":"2016-10-06","value":"0"},{"dateTime":"2016-10-07","value":"0"},{"dateTime":"2016-10-08","value":"0"},{"dateTime":"2016-10-09","value":"0"},{"dateTime":"2016-10-10","value":"0"},{"dateTime":"2016-10-11","value":"0"},{"dateTime":"2016-10-12","value":"0"},{"dateTime":"2016-10-13","value":"0"},{"dateTime":"2016-10-14","value":"0"},{"dateTime":"2016-10-15","value":"0"},{"dateTime":"2016-10-16","value":"0"},{"dateTime":"2016-10-17","value":"0"},{"dateTime":"2016-10-18","value":"0"},{"dateTime":"2016-10-19","value":"0"},{"dateTime":"2016-10-20","value":"0"},{"dateTime":"2016-10-21","value":"0"},{"dateTime":"2016-10-22","value":"0"},{"dateTime":"2016-10-23","value":"0"},{"dateTime":"2016-10-24","value":"0"}]}


My Model Class:

class StepDataModel {
ArrayList<Activities_step > activities[];
// ArrayList<Activities_step> activities_steps = new ArrayList<activities_step>();
public ArrayList<Activities_step>[] getActivities() {
return activities;
}
public void setActivities(ArrayList<Activities_step>[] activities) {
this.activities = activities;
}


class Activities_step {
public String dateTime;
public String value;

public String getDateTime() {
return dateTime;
}

public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}}}


I am trying to store json data in activity using:

StepDataModel stepDataModel = gson.fromJson(result.toString(), StepDataModel.class);


Can anyone please tell me where i am doing wrong?

Answer

Check this:

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class ActivitiesStep {

@SerializedName("dateTime")
@Expose
private String dateTime;
@SerializedName("value")
@Expose
private String value;

/**
* 
* @return
* The dateTime
*/
public String getDateTime() {
return dateTime;
}

/**
* 
* @param dateTime
* The dateTime
*/
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}

/**
* 
* @return
* The value
*/
public String getValue() {
return value;
}

/**
* 
* @param value
* The value
*/
public void setValue(String value) {
this.value = value;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class Example {

@SerializedName("activities-steps")
@Expose
private List<ActivitiesStep> activitiesSteps = new ArrayList<ActivitiesStep>();

/**
* 
* @return
* The activitiesSteps
*/
public List<ActivitiesStep> getActivitiesSteps() {
return activitiesSteps;
}

/**
* 
* @param activitiesSteps
* The activities-steps
*/
public void setActivitiesSteps(List<ActivitiesStep> activitiesSteps) {
this.activitiesSteps = activitiesSteps;
}

}