带有GSON的Android Volley库,如何将Json对象转换为数组

时间:2018-03-23 12:24:24

标签: java android json gson

我从API获取数据,有些字段在json对象中我想将其转换为json Arry。我无法修改json,但我可以将其存储为json数组。

我已经创建了pojo类。现在在json Response 4产品即将推出,将来它会增加列表,所以通过json对象我无法获得产品的价值。

我的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",

    ]
  }
 }

在上面的json响应qty中,cunsumption_timesold_qty位于以下对象中:

"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,

    },

我想存储上面json的vaules,如下所示:

"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,

    ],

片段代码:

 HighVelocityPojo highVelocityPojo;
 ArrayList<Top_10_BouquitShowPojo> graphList;

 private void JsonRequestGraph() {

    utils.showDialog();

    String url = Constants.TOP_10_BOUQUIT_HV+"days="+time+"&warehouse_id="+whareHouse;
    Log.e("URL", "" + url);

    JsonObjectRequest request = new JsonObjectRequest(url, null,
            response -> {
                Log.e("onResponse",""+response);
                try {

                    // Gson Code Here
                    Gson gson = new Gson();
                    Type listType = new TypeToken<HighVelocityPojo>() {
                    }.getType();

                    highVelocityPojo = gson.fromJson(response.toString(), listType);
                    Log.e("size", highVelocityPojo.getSKU().size() + "");

                    graphList = new ArrayList<>();
                    for(int i=0; i<highVelocityPojo.getSKU().size();i++){
                        graphList.add(new Top_10_BouquitShowPojo(highVelocityPojo.getWherehouse3().getProductname().get(i), highVelocityPojo.getWherehouse3().getData().get(i)));
                        Log.e("i VALUE 0",""+i+"----"+graphList.size());

                        if(i == highVelocityPojo.getSKU().size()-1){
                            callGraphView();
                            Log.e("i VALUE",""+i);

                        }
                    }

                } 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);

}

HighVelocityPojo:

 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;
    }

    }

WhareHouse3_HV:

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;
    }
    ...........

Qty_HV:

public class Qty_HV {

    @SerializedName("10 Blue Dendrobium Orchids( Z8363 )")
    @Expose
    private Integer _10BlueDendrobiumOrchidsZ8363;
    @SerializedName("Rose 6 St Orange Bqt in Foam Disks 50 cm ( Z8597 )")
    @Expose
    private Integer rose6StOrangeBqtInFoamDisks50CmZ8597;
    @SerializedName("5 Blue, 3 Purple, 2 Yellow D Orchids( Z8548 )")
    @Expose
    private Integer _5Blue3Purple2YellowDOrchidsZ8548;
    @SerializedName("12 Rainbow Roses( Z7031 )")
    @Expose
    private Integer _12RainbowRosesZ7031;

    public Integer get10BlueDendrobiumOrchidsZ8363() {
        return _10BlueDendrobiumOrchidsZ8363;
    }
     ..............

0 个答案:

没有答案