我想閱讀sub_category> ID,從JSON使用改造名稱嵌套的Json使用改造
但是,下面的代碼給失敗的響應。
java.lang.IllegalStateException:預期BEGIN_OBJECT但 BEGIN_ARRAY第2行2列路徑$
的json ....
[
{
"id": "1",
"name": "Invamore",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "102",
"name": "3 sided Dangler"
},
{
"id": "103",
"name": "Leaflet"
}
]
},
{
"id": "2",
"name": "HUPS",
"sub_category": [
{
"id": "103",
"name": "Leaflet"
},
{
"id": "104",
"name": "Posters"
},
{
"id": "105",
"name": "Sunpack"
}
]
},
{
"id": "3",
"name": "Xplore",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "103",
"name": "Leaflet"
},
{
"id": "106",
"name": "Dangler"
},
{
"id": "107",
"name": "Vertical Streamer"
}
]
},
{
"id": "4",
"name": "Xpress",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "103",
"name": "Leaflet"
},
{
"id": "108",
"name": "Streamer"
}
]
},
{
"id": "5",
"name": "Matrix",
"sub_category": [
{
"id": "103",
"name": "Leaflet"
}
]
},
{
"id": "6",
"name": "Instabrite",
"sub_category": [
{
"id": "103",
"name": "Leaflet"
}
]
},
{
"id": "7",
"name": "Mileage",
"sub_category": [
{
"id": "107",
"name": "Vertical Streamer"
}
]
},
{
"id": "8",
"name": "Onam Posm",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "106",
"name": "Dangler"
}
]
}]
Backend.java
public void pos_func(String user_id) {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call call = apiService.POS_MODEL_CALL(user_id);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
}
@Override
public void onFailure(Call call, Throwable t) {
Log.d("sk_log", "Failed! Error = " + t.getMessage());
}
});
}
ApiInterface.java
@FormUrlEncoded
@POST("pos_distributed.php")
Call<ValuesPos> POS_MODEL_CALL(@Field("user_id") String user_id);
型號:從pojoschema2pojo.com以上JSON
ValuesPos.java
package com.example.shkhan.myapp.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
/**
* Created by shkhan on 14/11/16.
*/
public class ValuesPos {
@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("sub_category")
@Expose
public List<SubCategory> subCategory = new ArrayList<SubCategory>();
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The subCategory
*/
public List<SubCategory> getSubCategory() {
return subCategory;
}
/**
*
* @param subCategory
* The sub_category
*/
public void setSubCategory(List<SubCategory> subCategory) {
this.subCategory = subCategory;
}
}
SubCategory.json
package com.example.shkhan.myapp.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by shkhan on 14/11/16.
*/
public class SubCategory {
@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
}
還是同樣的錯誤 –
錯誤:java.lang.IllegalStateException:預期BEGIN_OBJECT但BEGIN_ARRAY第2行2列路徑$ –
當我讀取錯誤時,我認爲問題出在你的ApiInterface.java上,請嘗試多個東西而不是'調用 ...' –
compte14031879