2013-08-26 228 views
0

我需要为Arraylist创建一个JSON对象。下面是代码创建一个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"}]} 

上述目标在每行只有最后一个项目。

+1

mh ...缺少{array.put(json);之后; –

+0

感谢您指出这一点,但它已经在代码中。在粘贴时出现错字错误。 – Gaurav

回答

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

非常感谢。现在正在工作。 – Gaurav

1

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

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

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

最后,也许你应该考虑有一个功能,读取/从OrderDetailsData写了一个JSON对象,这样就可以重新使用在其他web服务的代码。

+0

谢谢。它与建议的更改一起工作。 – Gaurav

0

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

0

看看这个我试图使用GSON库,试试这个,我没有测试过。这应该很好。

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