【问题标题】:Create an array of JSON Objects创建 JSON 对象数组
【发布时间】:2013-08-28 20:15:13
【问题描述】:

我需要为 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"}]}

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

【问题讨论】:

  • mh... 在 array.put(json); 之后缺少一个 {
  • 感谢您指出这一点,但它已经存在于代码中。在此处粘贴时出现拼写错误。

标签: android json


【解决方案1】:

下面的 sn-p 将在单个语句中创建 json 对象数组,它甚至在使用 Google 的 Gson 库从对象创建 json 时执行 null 检查。

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

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

【讨论】:

    【解决方案2】:

    看看这个我正在尝试使用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】:

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

      【讨论】:

        【解决方案4】:

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

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

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

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

        【讨论】:

          【解决方案5】:

          小错误

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

          【讨论】:

            猜你喜欢
            • 2018-07-03
            • 1970-01-01
            • 2013-07-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-22
            相关资源
            最近更新 更多