如何将JSONObjects放在JsonArrays中

时间:2017-04-28 06:56:12

标签: android

你好我想尝试使用POST方法来获取类似这样的字符串

{"Date":"01-04-2017","PartyName":"Customer3","Supplier":"Supplier2",
"Items":[{"ItemNo":0,"ItemName":"a","Quantity":100,"DueDate":"01-04-
2017","Price":80,"Amount":80000}]}

这是我的代码:

JSONObject singleorder= new JSONObject();
    try {
        singleorder.put("Date",Get_date);
        singleorder.put("PartyName",Get_partyname);
        singleorder.put("Supplier", Get_supplier);


       JSONArray arr = singleorder.getJSONArray("Items");
        for(int i=0;i<arr.length();i++){
            JSONObject obj = arr.getJSONObject(i);
            //obj.put("ItemNo",Get_itemno);
            obj.put("ItemName",Get_itemname);
            obj.put("Quantity",Get_quantity);
            obj.put("Price",Get_price);
            obj.put("Amount",Get_amount);
        }

        //JSONArray item = new JSONArray("Items");



    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

当我运行我的代码时,不会使用数组“项目”。它给了我错误:

org.sjon.JSONException:Items没有值。

我做错了什么?

4 个答案:

答案 0 :(得分:2)

JSONObject singleorder= new JSONObject();
    try {
        singleorder.put("Date",Get_date);
        singleorder.put("PartyName",Get_partyname);
        singleorder.put("Supplier", Get_supplier);

       int loopSize = Get_No_items(); //Or just initialize it as 1 in your particular case
       JSONArray arr = new JSONArray();
        for(int i=0;i<loopSize;i++){
            JSONObject obj = new JSONObject();
            //obj.put("ItemNo",Get_itemno);
            obj.put("ItemName",Get_itemname);
            obj.put("Quantity",Get_quantity);
            obj.put("Price",Get_price);
            obj.put("Amount",Get_amount);
            arr.put(obj);
        }

        singleorder.put("Items", arr);



    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

答案 1 :(得分:0)

您尚未将项目添加到singleorder

你用这样的一行来做:

singleorder.add(key, value);

答案 2 :(得分:0)

做这样的事情,它会更加无差错

class A implements Serializable{
private String name;
private B addresses[];

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public B[] getAddresses() {
    return addresses;
}
public void setAddresses(B[] addresses) {
    this.addresses = addresses;
}}

class B implements Serializable{
private String address;

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

最后

    {A a = new A();
    a.setName("abc");

    B addresses[] = new B[1];
    addresses[0] = new B();
    addresses[0].setAddress("xyz");

    a.setAddresses(addresses);

    ObjectMapper mapper = new ObjectMapper();

    String jsonString = mapper.writeValueAsString(a);}

它将生成类似

的JSON字符串

<强> {&#34;名称&#34;:&#34; Zafa公司&#34;&#34;地址&#34;:[{&#34;地址&#34;:&# 34; XYZ&#34;}]}

答案 3 :(得分:0)

您可以像这样创建json数组

  

private JSONArray getJsonArray(ArrayList<Object> array) { JSONArray jsonArray = new JSONArray(); if (array != null && array.size() > 0) { for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = new JSONObject(); Object object = array.get(i); try { jsonObject.put(DESCRIPTION, object.getData()); jsonObject.put(IMAGE_URL, object.getAnotherData()); jsonArray.put(i, jsonObject); } catch (JSONException e) { e.printStackTrace(); } } } return jsonArray; }
private JSONArray getJsonArray(ArrayList<Object> array) { JSONArray jsonArray = new JSONArray(); if (array != null && array.size() > 0) { for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = new JSONObject(); Object object = array.get(i); try { jsonObject.put(DESCRIPTION, object.getData()); jsonObject.put(IMAGE_URL, object.getAnotherData()); jsonArray.put(i, jsonObject); } catch (JSONException e) { e.printStackTrace(); } } } return jsonArray; }