创建一个JSON对象数组

时间:2013-08-26 10:56:01

标签: android json

我需要为Arraylist创建一个JSON对象。以下是代码

public boolean submitOrder(ArrayList<OrderDetailsData> orderList) {

        serUri = "lists.json";
        method = "post";


        JSONObject jsonObject = new JSONObject();

        JSONObject json = new JSONObject();

        JSONArray array = new JSONArray();
        try {
            for (int i = 0; i < orderList.size(); i++) {
                json.put("orderno", orderList.get(i).getOrderNumber()
                        .toString());
                json.put("tableno", orderList.get(i).getTableNumber()
                        .toString());
                json.put("itemname", orderList.get(i).getItemName().toString());
                json.put("amount", orderList.get(i).getAmount().toString());

                json.put("ordstatus", orderList.get(i).getOrderStatus()
                        .toString());
                array.put(json);



            }

         catch (JSONException je) {
            return false;
        }

        try {

            jsonObject.put("list", array);
        } catch (JSONException je) {
            return false;
        }

        WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();
        webServiceTask.execute(serUri, method,jsonObject, this);
        return true;
    }

问题是它的创建对象包含每个位置的最后一行的详细信息。假设我的orderList有3行,创建的jsonObject有3行,但是所有3行都有第3行数据。看起来像是所有行的最重要数据的取代数据。我尝试了几种其他方式,但仍然没有得到理想的结果。请指教。感谢。

创建了JSON对象:

{"list":[{"amount":"10.50","orderno":"0220130826163623","quantity":"1","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"}]}

上述对象每行只有最后一项。

5 个答案:

答案 0 :(得分:1)

小错误

try {
            for (int i = 0; i < orderList.size(); i++) {

                JSONObject json = new JSONObject(); // update here

                json.put("orderno", orderList.get(i).getOrderNumber()
                        .toString());
                json.put("tableno", orderList.get(i).getTableNumber()
                        .toString());
                json.put("itemname", orderList.get(i).getItemName().toString());
                json.put("amount", orderList.get(i).getAmount().toString());

                json.put("ordstatus", orderList.get(i).getOrderStatus()
                        .toString());
                array.put(json);



            }

         catch (JSONException je) {
            return false;
        }

答案 1 :(得分:1)

您的JSONObject json = new JSONObject();应该在循环中。

此外,每次访问属性(性能)时都不应该执行get(i)。您可以使用for循环的其他构造:

for (OrderDetailsData data : orderList) {
  JSONObject json = new JSONObject();
  json.put("orderno", data.getOrderNumber().toString());
  // ...
}

最后,也许您应该考虑使用一个从OrderDetailsData读取/写入JSON对象的函数,以便您可以重用其他Web服务中的代码。

答案 2 :(得分:1)

看看这个我正在尝试使用GSON lib,尝试一下,我没有经过测试。这应该可以正常工作。

从Json字符串到Json对象:

String jsonStr = "{\"a\": \"A\"}";

Gson gson = new Gson();
JsonElement element = gson.fromJson (jsonStr, JsonElement.class);
JsonObject jsonObj = element.getAsJsonObject();

对于您的代码,这应该可以正常工作:

public boolean submitOrder(ArrayList<OrderDetailsData> orderList) 
{
    serUri = "lists.json";
    method = "post";

    Gson gson = new Gson();
    String jsonstr = new Gson().toJson(orderList);
    JsonElement element = gson.fromJson (jsonStr, JsonElement.class);
    JsonObject jsonObject = element.getAsJsonObject();

    WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();
    webServiceTask.execute(serUri, method,jsonObject, this);
    return true;
}

答案 3 :(得分:1)

以下代码片段将在单个语句中创建json对象数组,它甚至在使用Google的Gson库从对象创建json时执行空检查。

public boolean submitOrder(ArrayList<OrderDetailsData> orderList) {
    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    JsonElement ordersObj = gson.toJsonTree(orderList);
    myObj.add("list", ordersObj);
}

答案 4 :(得分:0)

您需要在for循环中实例化json对象。