3

I am trying to parse my own JSON, but getting JSONSyntaxException, here is how my JSON looks:

{
    "type":"success",
    "value":[
        {
            "id":1,
            "title":"Title - 1",
         "name":{
            "first":"First - 1",
            "last":"Last - 1"
         },
            "hobbies":[
                "Writing Code - 1",
            "Listening Music - 1"
            ]
        },
       .....
    ]
}

Log says:

E/app.retrofit_chucknorries.MainActivity$2: ERROR: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 
at line 7 column 12 path $.value[0].name
01-21 12:41:52.156 28936-28936/app.retrofit_chucknorries 
W/System.err: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 
at line 7 column 12 path $.value[0].name

Where I am doing mistake ? I just made few small modifications as per my requirement and classes else everything almost same as in original code Value.java:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class Value {

    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("hobbies")
    @Expose
    private List<String> hobbies = new ArrayList<String>();

    @SerializedName("name")
    @Expose
    private List<Name> name = new ArrayList<Name>();

    public Integer getId() {
        return id;
    }

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

    public List<Name> getName() {
        return name;
    }

    public void setName(List<Name> name) {
        this.name = name;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

1 Answer 1

4

In your Master.java class, your name is not an array!

private List<Name> name = new ArrayList<Name>();

Change to this instead and try:

 private Name name;

Actually by seeing the logs of the exception you can tell this.

Sign up to request clarification or add additional context in comments.

7 Comments

The log says: "BEGIN_ARRAY but was BEGIN_OBJECT at line 7", as your line 7 of your Json should be this line: "name":{
yeah thank you I did mistake. I ticked as useful and will accept in 5 mins, Can I ask a question ?
check an updated screenshot above, I want to know How Can I get Hobbies and Name from JSON.....
To get name: for(Value value : values) {String name = value.getName()}
but I am using two different - different textviews for first name and last name, please check above screen.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.