Expected BEGIN_OBJECT but was BEGIN_ARRARY at line 1 column 2 path$異常解決方法
阿新 • • 發佈:2019-01-02
[
{
"id": 1,
"name": "移動開發",
"alias": "mobile"
},
{
"id": 14,
"name": "Web前端",
"alias": "web"
},
{
"id": 15,
"name": "架構設計",
"alias": "enterprise"
},
{
"id": 16,
"name": "程式語言",
"alias ": "code"
},
{
"id": 17,
"name": "網際網路",
"alias": "www"
},
{
"id": 6,
"name": "資料庫",
"alias": "database"
},
{
"id": 12,
"name": "系統運維",
"alias": "system"
},
{
"id": 2,
"name": "雲端計算" ,
"alias": "cloud"
},
{
"id": 3,
"name": "研發管理",
"alias": "software"
},
{
"id": 19,
"name": "特約專家",
"alias": "invite"
},
{
"id": 8,
"name": "行業名家",
"alias": "celebrity"
},
{
"id": 7 ,
"name": "綜合",
"alias": "other"
},
{
"id": 20,
"name": "名譽專家",
"alias": "honour"
}
]
我解析以上json資料的時候,報出
java.lang.illegalStateException:Expected BEGIN_OBJECT but was BEGIN_ARRARY at line 1 column 2 path$
發現自己解析的時候,使用的是物件,而非陣列。但是以上json是陣列形式
package com.android.csdnclient.bean;
import com.google.gson.annotations.SerializedName;
/**
* 部落格系統分類
*/
public class ChannelBean {
//分類id
@SerializedName("id")
private int id;
//分類名稱
@SerializedName("name")
private String name;
//分類別名
@SerializedName("alias")
private String alias;
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.android.csdnclient.retrofit;
import com.android.csdnclient.bean.ChannelBean;
import java.util.List;
import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;
/**
* 網路請求
*/
public interface CSDNService {
//獲取部落格系統分類
@GET("/blog/getchannel")
Observable<List<ChannelBean>> getChannel(@Query("client_id") String client_id);
}
Observable<List<ChannelBean>>
這裡必須使用物件容器來儲存json陣列。