我希望使用 Gson Library 解析json
以下,从最后两个小时开始,我尝试将POJO类中的get数据分别放在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中得到回复
Log.e("onResponse",""+response);
但无法将数据设置为POJO。
POJO CLASSES:
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;
}
}
答案 0 :(得分:2)
你的json中似乎没有“list”键,所以这会产生错误:
String info = response.getString("list");
JSONArray jsonArray = new JSONArray(info);
尝试传递响应,如下所示:
Gson gson = new Gson();
Type listType = new TypeToken<HighVelocityPojo>() {
}.getType();
HighVelocityPojo highVelocityPojo = gson.fromJson(response.toString(), listType);
Log.e("size", highVelocityPojo.getSKU().size() + "");
完整代码:
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,
response -> {
Log.e("onResponse",""+response);
try {
Gson gson = new Gson();
Type listType = new TypeToken<HighVelocityPojo>() {
}.getType();
HighVelocityPojo highVelocityPojo = gson.fromJson(response.toString(), listType);
Log.e("size", highVelocityPojo.getSKU().size() + "");
callGraphView();
} catch (Exception e) {
Log.e("Exception",""+e);
utils.hideDialog();
e.printStackTrace();
}
utils.hideDialog();
}, 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);
}