本文介绍了改造-JSON数组解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Retrofit解析JSON Array,其中JSON数组如下:

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

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

这是我的JSON的样子:

{
    "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());

运行程序时,我是getting data,如下所示:

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

[Music, Reading]

所以,问题Why,我的输出也得到了[].

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();
}

推荐答案

实际上value.getHobbies();直接使用[ ]返回List.更好的iterate它并使用StringBuilder

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

这篇关于改造-JSON数组解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:35