我對JSON解析相當陌生,我使用Square的Retrofit庫並且遇到了這個問題。改造期望的BEGIN_OBJECT,但是BEGIN_ARRAY
我試圖解析此JSON的回覆:
[
{
"id": 3,
"username": "jezer",
"regid": "oiqwueoiwqueoiwqueoiwq",
"url": "http:\/\/192.168.63.175:3000\/users\/3.json"
},
{
"id": 4,
"username": "emulator",
"regid": "qwoiuewqoiueoiwqueoq",
"url": "http:\/\/192.168.63.175:3000\/users\/4.json"
},
{
"id": 7,
"username": "test",
"regid": "ksadqowueqiaksj",
"url": "http:\/\/192.168.63.175:3000\/users\/7.json"
}
]
這裏是我的模型:
public class Contacts {
public List<User> contacts;
}
...
public class User {
String username;
String regid;
@Override
public String toString(){
return(username);
}
}
我的接口:
public interface ContactsInterface {
@GET("/users.json")
void contacts(Callback<Contacts> cb);
}
我的成功方法:
@Override
public void success(Contacts c, Response r) {
List<String> names = new ArrayList<String>();
for (int i = 0; i < c.contacts.size(); i++) {
String name = c.contacts.get(i).toString();
Log.d("Names", "" + name);
names.add(name);
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, names);
mSentTo.setAdapter(spinnerAdapter);
}
當我用我的成功方法,它引發錯誤
預計BEGIN_OBJECT但BEGIN_ARRAY在行1列2
什麼是錯在這裏?
尼斯回答AMIGO –
如何改造2解決這個問題? –
@AzlanJamal同樣的方式 –