简体   繁体   中英

Retrofit - JSON Array Parsing

I am parsing JSON Array using Retrofit , where JSON Array looks like:

"hobbies": [ "Music", "Reading"]

Here is what my JSON looks like:

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

Value.java

private List<String> hobbies = new ArrayList<String>();

Adapter.java

viewHolder.hobbies.setText(value.getHobbies().toString());

And when I run my program, I am getting data as seen below:

[Music, Reading]

So, Question is Why I am getting [] as well in output.

Value.java

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 Name name;

    .....

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

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

Service.java

public interface Service {

   @GET("/demo_retrofit.json")
   Observable<Master> getMaster();
}

Actually value.getHobbies(); directly return an List with [ ] . better iterate it and show by using StringBuilder

MD and Jackson are right. Object.toString() method returns a string that "textually represents" this object. So for your case, hobbies is an arrayList so the textually representation will have [] with it. ;)

Edit:

You may want to use for loop for this:

for (String hobby : hobbies) {
    // Do something with hobby here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM