Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...