【发布时间】:2018-03-22 11:56:17
【问题描述】:
我想通过使用 Gson 库 解析下面的json,从最近两个小时开始,我正在尝试获取 POJO 类中对应于下面 json 的数据。
我已经使用 http://www.jsonschema2pojo.org/ 为下面的 JSON 创建了 POJO/Model 类,但我无法使用 GSON 解析它。
我的 JSON:
{
"SKU": [
"Hydrangea White( F033 )",
"Pink Rose 50 cm( F075 )",
"Yellow Rose 50 cm( F074 )",
"White Rose 50 cm( F072 )",
],
"wherehouse3": {
"name": "New Jersey",
"data": [
20,
14.583333333333,
13.541666666667,
12.5,
],
"qty": {
"Hydrangea White( F033 )": 70,
"Pink Rose 50 cm( F075 )": 50,
"Yellow Rose 50 cm( F074 )": 50,
"White Rose 50 cm( F072 )": 25,
},
"cunsumption_time": {
"Hydrangea White( F033 )": 0,
"Pink Rose 50 cm( F075 )": 0,
"Yellow Rose 50 cm( F074 )": 0,
"White Rose 50 cm( F072 )": 0,
},
"sold_qty": {
"Hydrangea White( F033 )": 480,
"Pink Rose 50 cm( F075 )": 350,
"Yellow Rose 50 cm( F074 )": 325,
"White Rose 50 cm( F072 )": 300,
},
"projected_inventry": [
0,
0,
0,
0,
],
"recived_inventry": [
1120,
525,
2953,
900,
],
"sku": [
"F033",
"F075",
"F074",
"F072",
],
"productname": [
"Hydrangea White",
"Pink Rose 50 cm",
"Yellow Rose 50 cm",
"White Rose 50 cm",
]
}
}
当我尝试使用下面的代码时,它会引发异常
异常:org.json.JSONException:列表没有值
片段代码:
private static final int MY_SOCKET_TIMEOUT_MS = 60000;
List<HighVelocityPojo> mList;
private void JsonRequestGraph() {
utils.showDialog();
String url = Constants.TOP_10_BOUQUIT+"days="+time+"&warehouse_id="+whareHouse;
Log.e("URL", "" + url);
JsonObjectRequest request = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("onResponse",""+response);
try {
String info = response.getString("list");
JSONArray jsonArray = new JSONArray(info);
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<HighVelocityPojo>>() {
}.getType();
mList = gson.fromJson(jsonArray.toString(), listType);
callGraphView();
} catch (Exception e) {
Log.e("Exception",""+e); //Here getting Exception , org.json.JSONException: No value for list
utils.hideDialog();
e.printStackTrace();
}
utils.hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error",""+error.getMessage());
utils.hideDialog();
}
});
request.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance(getContext()).addToRequestQueue(request);
}
我能够在日志中得到响应
Log.e("onResponse",""+response);但无法将数据设置为 POJO。
POJO 类:
HighVelocityPojo.java
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class HighVelocityPojo {
@SerializedName("SKU")
@Expose
private List<String> sKU = null;
@SerializedName("wherehouse3")
@Expose
private Wherehouse3_HV wherehouse3;
public List<String> getSKU() {
return sKU;
}
public void setSKU(List<String> sKU) {
this.sKU = sKU;
}
public Wherehouse3_HV getWherehouse3() {
return wherehouse3;
}
public void setWherehouse3(Wherehouse3_HV wherehouse3) {
this.wherehouse3 = wherehouse3;
}
}
Wherehouse3_HV.java
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Wherehouse3_HV {
@SerializedName("name")
@Expose
private String name;
@SerializedName("data")
@Expose
private List<Double> data = null;
@SerializedName("qty")
@Expose
private Qty_HV qty;
@SerializedName("cunsumption_time")
@Expose
private CunsumptionTime_HV cunsumptionTime;
@SerializedName("sold_qty")
@Expose
private SoldQty_HV soldQty;
@SerializedName("projected_inventry")
@Expose
private List<Integer> projectedInventry = null;
@SerializedName("recived_inventry")
@Expose
private List<Integer> recivedInventry = null;
@SerializedName("sku")
@Expose
private List<String> sku = null;
@SerializedName("productname")
@Expose
private List<String> productname = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Double> getData() {
return data;
}
public void setData(List<Double> data) {
this.data = data;
}
public Qty_HV getQty() {
return qty;
}
public void setQty(Qty_HV qty) {
this.qty = qty;
}
public CunsumptionTime_HV getCunsumptionTime() {
return cunsumptionTime;
}
public void setCunsumptionTime(CunsumptionTime_HV cunsumptionTime) {
this.cunsumptionTime = cunsumptionTime;
}
public SoldQty_HV getSoldQty() {
return soldQty;
}
public void setSoldQty(SoldQty_HV soldQty) {
this.soldQty = soldQty;
}
public List<Integer> getProjectedInventry() {
return projectedInventry;
}
public void setProjectedInventry(List<Integer> projectedInventry) {
this.projectedInventry = projectedInventry;
}
public List<Integer> getRecivedInventry() {
return recivedInventry;
}
public void setRecivedInventry(List<Integer> recivedInventry) {
this.recivedInventry = recivedInventry;
}
public List<String> getSku() {
return sku;
}
public void setSku(List<String> sku) {
this.sku = sku;
}
public List<String> getProductname() {
return productname;
}
public void setProductname(List<String> productname) {
this.productname = productname;
}
}
【问题讨论】:
-
你能把你的 Wherehouse3_HV POJO 也展示一下吗?
-
感谢您的回复,我用 Wherehouse3_HV POJO 类更新了我的问题。@LeviAlbuquerque
标签: java android android-volley android-json gson