Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson: Expected begin_array but was STRING

I am facing the following error in parsing JSON data:

Expected begin_array but was STRING at line 1 column 34

I cannot find a solution. My JSON is the following:

   {"result":0,"count":2,"records":"[{\"name\":\"name1\",
    \"id\":\"28\",
 \"photo\":\"\\\/gallery\\\/c9\\\/f0f8\\\/95fb\\\/c9f0f895fb98ab9159f51fd0297e236d\\\/28\\\/tb_uykzubjqmxbv6zkogdsd_c64962310e572f5e8a4c73a44a4fa3dd.jpg\"},{\"name\":\"name2\",
\"id\":\"134\",
\"photo\":\"\\\/gallery\\\/c9\\\/f0f8\\\/95fb\\\/c9f0f895fb98ab9159f51fd0297e236d\\\/134\\\/tb_23ffxuw9-5ys4iqtwztz_610982fa52cca03412dbc84ab0ea5e18.jpg\"}]"}

This is my PersonContent Class:

public class PersonContent {

    @SerializedName("result")
    public int result;
    @SerializedName("count")
    public int  count;
    @SerializedName("records")
    List<Person> records;


    public List<Person> getRecords() {
        return records;
    }

    public void setRecords(List<Person> records) {
        this.records = records;
    }

    public void setResults(int result) {
        this.result = result;
    }

    public int getResults(){
        return result;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount(){
        return count;
    }

and the following is the Person Class:

public class Person implements Serializable {
        private static final long serialVersionUID = 1L;

        @SerializedName("name")
        public String name;
        @SerializedName("id")
        public String id;
        @SerializedName("photo")
        public String photo;


        public Person(){

        }
        public Person( String name,String id,String photo) {
            this.id = id;
            this.name = name;
            this.photo = photo;

        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhoto() {
            return photo;
        }

        public void setPhoto(String photo) {
            this.photo = photo;
        }

    @Override
    public String toString() {
        return name;
    }
}

And here is the code where i Deserialize the previous mentioned JSON Data

private void Parse(){
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);  
personlist = gson.fromJson(reader, PersonContent.class);
}

I have tried all solutions found in here, but I could not find an identical JSON. Also the following:com.google.gson.JsonSyntaxException: Expected BEGIN_ARRAY but was STRING

like image 975
klgr Avatar asked Apr 01 '14 19:04

klgr


1 Answers

the error is in the json you receive: you class expects an array because

 List<Person> records;

but then in your json you have

"records":"[

it has to be :

"records": [

and at the end ]"}, has to be

]}
like image 121
kalin Avatar answered Sep 19 '22 15:09

kalin