[英]Retrofit - JSON Array Parsing
I am parsing JSON Array
using Retrofit
, where JSON Array looks like: 我正在使用
Retrofit
解析JSON Array
,其中JSON Array看起来像:
"hobbies": [ "Music", "Reading"]
Here is what my JSON
looks like: 这是我的
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 Value.java
private List<String> hobbies = new ArrayList<String>();
Adapter.java Adapter.java
viewHolder.hobbies.setText(value.getHobbies().toString());
And when I run my program, I am getting data
as seen below: 当我运行程序时,我正在
getting data
,如下所示:
[Music, Reading]
So, Question is Why
I am getting []
as well in output. 因此, 问题是
Why
我的输出也得到[]
。
Value.java 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 Service.java
public interface Service {
@GET("/demo_retrofit.json")
Observable<Master> getMaster();
}
Actually value.getHobbies();
实际上是
value.getHobbies();
directly return an List
with [ ]
. 用
[ ]
直接返回一个List
。 better iterate
it and show by using StringBuilder
更好地
iterate
并通过使用StringBuilder
iterate
显示
MD and Jackson are right. MD和杰克逊是对的。
Object.toString()
method returns a string that "textually represents" this object. Object.toString()
方法返回一个“以文本形式表示”此对象的字符串。 So for your case, hobbies is an arrayList so the textually representation will have []
with it. 因此,对于您的情况,兴趣爱好是一个arrayList,因此文本表示形式将带有
[]
。 ;) ;)
Edit: 编辑:
You may want to use for loop for this: 您可能要为此使用for循环:
for (String hobby : hobbies) {
// Do something with hobby here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.