2013-03-19 55 views
1

我在寫JSON,但我並沒有做出正確的格式 它給我的只有用戶和最後一個屬性項的拳頭屬性,而不是給我完整的JSON .. 提前感謝.. 我的代碼是在陣列JSON沒有給我競爭的格式,

JSONObject json = new JSONObject(); 

     try { 
      JSONObject subJson = new JSONObject(); 
      //JSONArray jsonArray = new JSONArray(); 

      subJson.put("saleManId", LogIn.UserId); 
      subJson.put("customerId", LogIn.CustomerId); 

      json.put("User", subJson); 

      for (int i = 0; i < LogIn.subValues.length; i++) { 
       JSONObject subSubObj = new JSONObject(); 

       for (int j = 0; j < LogIn.subValues[i].length; j++) { 
        if (LogIn.subValues[i][j] > 0) { 

         JSONObject item = new JSONObject(); 

         item.put("itemID", LogIn.subIds[i][j]); 
         item.put("qty", LogIn.subValues[i][j]); 

         subSubObj.put("item", item); 

        } 

       } 


       if(subSubObj.length()>0) 
       { 
        json.put("orderDetail", subSubObj); 
        Log.d("json", json.toString()); 
       } 


      } 
      Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString()); 

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

他們是30到35項,但它給我的只有這

{"orderDetail":{"item":{"qty":1,"itemID":"60"}},"User":{"customerId":"460","saleManId":"1"}} 
+1

你需要一個JSONArray如果一個地方你想要的對象的集合 – gpasci 2013-03-19 08:13:30

+0

JSONArray只需要值,但我需要鍵也.. – 2013-03-19 08:31:01

+0

一個JSONObject可以有隻有一個值給定的鍵... – njzk2 2013-03-19 08:39:54

回答

0

我得到它... :)感謝所有...

JSONObject json = new JSONObject(); 

     try { 
      JSONObject subJson = new JSONObject(); 
      JSONArray jsonArray = new JSONArray(); 

      subJson.put("saleManId", LogIn.UserId); 
      subJson.put("customerId", LogIn.CustomerId); 

      json.put("User", subJson); 

      for (int i = 0; i < LogIn.subValues.length; i++) { 



       for (int j = 0; j < LogIn.subValues[i].length; j++) { 
        if (LogIn.subValues[i][j] > 0) { 

         JSONObject subSubObj = new JSONObject(); 
         JSONObject item = new JSONObject(); 

         item.put("itemID", LogIn.subIds[i][j]); 
         item.put("qty", LogIn.subValues[i][j]); 

         subSubObj.put("item", item); 

         jsonArray.put(subSubObj); 

        } 

       } 


      } 

      json.put("orderDetail", jsonArray); 
      Log.d("jsonnnnnnnnnnnnnnnnnnnnnnnnnnnnn", json.toString()); 

     } catch (JSONException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     sendString = json.toString(); 

出來把結果..

{"orderDetail":[ 
{"item":{"qty":1,"itemID":"4"}}, 
{"item":{"qty":1,"itemID":"14"}}, 
{"item":{"qty":1,"itemID":"28"}}, 
{"item":{"qty":1,"itemID":"59"}}, 
{"item":{"qty":1,"itemID":"60"}}, 
{"item":{"qty":1,"itemID":"62"}}, 
{"item":{"qty":1,"itemID":"24"}}, 
{"item":{"qty":1,"itemID":"36"}}, 
{"item":{"qty":1,"itemID":"51"}}], 
"User":{"customerId":"414","saleManId":"1"}} 
+0

JSONArray FTW;) – gpasci 2013-03-19 10:25:31

+0

yap JSonArray「) – 2013-03-25 07:42:48