Home Ask Login Register

Developers Planet

Your answer is one click away!

v4_adi February 2016

Gson throwing EXPECTED_ARRAY but found String Exception

I am using gson with retrofit to parse json response but I am getting the above mention exception. I have tried searching on net and figuring it out myself but haven't got any success till now

Here is my json response:

{
    "success": "true",
    "message": "",
    "data": [{
        "Id": "56B5D1408B8D9",
        "Name": "John",
        "Age":"28",
        "place": "Central City"
    }]
}

my Pojo Class

public class Datum {

@SerializedName("Id")
@Expose
private String Id;
@SerializedName("Name")
@Expose
private String Name;
@SerializedName("Age")
@Expose
private String Age;
@SerializedName("place")
@Expose
private String place;

/**
* 
* @return
* The Id
*/
public String getId() {
return Id;
}

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

/**
* 
* @return
* The Name
*/
public String getName() {
return Name;
}

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

/**
* 
* @return
* The Age
*/
public String getAge() {
return Age;
}

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

/**
* 
* @return
* The place
*/
public String getPlace() {
return place;
}

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

}

Parent Class 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;

@Generated("org.jsonschema2pojo")
public class UserModel {

@SerializedName("success")
@Expose
private String success;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Datum> data = new ArrayList<Datum>();

/**
* 
* @return
* The success
*/
public String getSuccess() {
return success;
}

/**
* 
* @param success
* The success
*/
public void setSuccess(String success) {
th        

Answers


keith February 2016

Gson will start working from the root, you'll need a parent that matches your response like:

class MyResponse {
    public List<Datum> data;
}

Your Datum class looks like it matches the items inside the data array in the response

Post Status

Asked in February 2016
Viewed 1,947 times
Voted 11
Answered 1 times

Search




Leave an answer


Quote of the day: live life