尝试使用Retrofit解析JSON响应时出现以下错误:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
这就是JSON响应的样子:
{
"automobi": [{
"Dealers": {
"Dealer": [{
"@attributes": {
"Id": "12",
"Name": "Barons Tokai",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "96ec67999ed613174efe17f697dc4695"
}
}, {
"@attributes": {
"Id": "13",
"Name": "Barons Pietermaritzburg",
"Region": "KwaZulu-Natal",
"Status": "active",
"UniqueKey": "0f0637a035a9bc98ac67aa30ffc1367e"
}
}, {
"@attributes": {
"Id": "36",
"Name": "Barons Bellville",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "9ec8f24fdee47f14ce56e9a3fc2689a4"
}
}, {
"@attributes": {
"Id": "37",
"Name": "Barons Bruma",
"Region": "Gauteng",
"Status": "active",
"UniqueKey": "de7ac02f51463d94cb11947c1bf5589c"
}
}, {
"@attributes": {
"Id": "38",
"Name": "Barons Culemborg",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "de2d98530d1ac95218c2eb5d25d847b2"
}
}, {
"@attributes": {
"Id": "39",
"Name": "Barons N1 City",
"Region": "Western Cape",
"Status": "active",
"UniqueKey": "a055a9a6fa1c9a43cced2335ce6eefcb"
}
}, {
"@attributes": {
"Id": "40",
"Name": "Barons Durban",
"Region": "KwaZulu-Natal",
"Status": "active",
"UniqueKey": "8e49cb3d88b54d144c6728a450b2f491"
}
}, {
"@attributes": {
"Id": "41",
"Name": "Barons Woodmead",
"Region": "Gauteng",
"Status": "active",
"UniqueKey": "097722a842f43f0c59cfac14694b8443"
}
}]
}
}]
}
这是我的经销商类:
public class Dealer {
@SerializedName("Id")
String mId;
@SerializedName("Name")
String mName;
@SerializedName("Region")
String mRegion;
@SerializedName("UniqueKey")
String mUniqueKey;
public String getId() {
return mId;
}
public void setId(String id) {
this.mId = id;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public String getRegion() {
return mRegion;
}
public void setRegion(String region) {
this.mRegion = region;
}
public String getUniqueKey() {
return mUniqueKey;
}
public void setUniqueKey(String uniqueKey) {
this.mUniqueKey = uniqueKey;
}
}
这是改装电话:
@FormUrlEncoded
@POST("get_dealer_list.php")
Call<List<Dealer>> getDealers(@Field("listname") String listName, @Field("outputformat") String outputFormat);
对于我做错的任何想法?
答案 0 :(得分:1)
对于最小的孩子
&#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 12&#34;, &#34;姓名&#34;:&#34; Barons Tokai&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 96ec67999ed613174efe17f697dc4695&#34; }
public class DealerObject {
@SerializedName("@attribute")
Dealer attribute;
public getAttribute() {
return attribute;
}
}
下一个
&#34;经销商&#34;:[ { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 12&#34;, &#34;姓名&#34;:&#34; Barons Tokai&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 96ec67999ed613174efe17f697dc4695&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 13&#34;, &#34;姓名&#34;:&#34; Barons Pietermaritzburg&#34;, &#34;地区&#34;:&#34;夸祖鲁 - 纳塔尔&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 0f0637a035a9bc98ac67aa30ffc1367e&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 36&#34;, &#34;姓名&#34;:&#34; Barons Bellville&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 9ec8f24fdee47f14ce56e9a3fc2689a4&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 37&#34;, &#34;姓名&#34;:&#34; Barons Bruma&#34;, &#34;地区&#34;:&#34;豪登&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; de7ac02f51463d94cb11947c1bf5589c&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 38&#34;, &#34;姓名&#34;:&#34; Barons Culemborg&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; de2d98530d1ac95218c2eb5d25d847b2&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 39&#34;, &#34;姓名&#34;:&#34;男爵N1城市&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; a055a9a6fa1c9a43cced2335ce6eefcb&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 40&#34;, &#34;姓名&#34;:&#34; Barons Durban&#34;, &#34;地区&#34;:&#34;夸祖鲁 - 纳塔尔&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 8e49cb3d88b54d144c6728a450b2f491&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 41&#34;, &#34;姓名&#34;:&#34; Barons Woodmead&#34;, &#34;地区&#34;:&#34;豪登&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 097722a842f43f0c59cfac14694b8443&#34; } } ]
public class DealerListObject {
List<DealerObject> Dealer;
public getDealer {
return Dealer;
}
}
Delers大对象
&#34;经销商&#34;:{ &#34;经销商&#34;:[ { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 12&#34;, &#34;姓名&#34;:&#34; Barons Tokai&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 96ec67999ed613174efe17f697dc4695&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 13&#34;, &#34;姓名&#34;:&#34; Barons Pietermaritzburg&#34;, &#34;地区&#34;:&#34;夸祖鲁 - 纳塔尔&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 0f0637a035a9bc98ac67aa30ffc1367e&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 36&#34;, &#34;姓名&#34;:&#34; Barons Bellville&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 9ec8f24fdee47f14ce56e9a3fc2689a4&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 37&#34;, &#34;姓名&#34;:&#34; Barons Bruma&#34;, &#34;地区&#34;:&#34;豪登&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; de7ac02f51463d94cb11947c1bf5589c&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 38&#34;, &#34;姓名&#34;:&#34; Barons Culemborg&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; de2d98530d1ac95218c2eb5d25d847b2&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 39&#34;, &#34;姓名&#34;:&#34;男爵N1城市&#34;, &#34;地区&#34;:&#34;西开普省&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; a055a9a6fa1c9a43cced2335ce6eefcb&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 40&#34;, &#34;姓名&#34;:&#34; Barons Durban&#34;, &#34;地区&#34;:&#34;夸祖鲁 - 纳塔尔&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 8e49cb3d88b54d144c6728a450b2f491&#34; } }, { &#34; @ attributes&#34;:{ &#34; Id&#34;:&#34; 41&#34;, &#34;姓名&#34;:&#34; Barons Woodmead&#34;, &#34;地区&#34;:&#34;豪登&#34;, &#34;状态&#34;:&#34;活跃&#34;, &#34; UniqueKey&#34;:&#34; 097722a842f43f0c59cfac14694b8443&#34; } } ] }
public class BigDealerObject {
List<DealerListObject> Dealers;
public getBigDealerObject {
return Dealers;
}
}
然后对所有回复:
public class DealerResponse {
List<BigDealerObject> automobi;
public getAutomobi {
return automobi;
}
}
最后,请致电:
Call<DealerResponse> getDealers(@Field("listname") String listName, @Field("outputformat") String outputFormat);
答案 1 :(得分:0)
不确定。正如您在尝试将其分配给列表(List)时,您可以看到JSON响应以对象({})开头。要解决这个问题,你应该创建一个名为Response的类,它将包含以下字段和类:
public class Response {
public DealersClass Dealers;
}
class DealersClass {
public List<Dealer> Dealer;
}
经销商类是您构建的类。一种避免将来出现此类错误的简单方法是查看您的回复。当你看到{这意味着你应该将它与Java类匹配。当你看到[这意味着你应该将它与Java List匹配。
希望它有所帮助。