2014-07-14 49 views
-4

我喜歡創建一個JSON格式發送到服務。我在下面提到的JSON格式。 產品對象來自循環。請假設我= 1並給我一個答案。如何在android中以我們自己的格式創建json格式?

{ 
    "tableid": 41, 
    "status": 141, 
    "products": [ 
     { 
      "menuitemid": 349, 
      "qty": "1", 
      "taxids": [ 
       { 
        "taxid": "1", 
        "Amount": 0.15 
       } 
      ], 
      "taxamount": 0.15, 
      "seatname": "Seat 1", 
      "modifiers": [], 
      "saleid": "140704131457005701", 
      "discountshiftlevelid": "" 
     }, 
     { 
      "menuitemid": 44, 
      "qty": "1", 
      "taxids": [ 
       { 
        "taxid": "1", 
        "Amount": 0.13425 
       } 
      ], 
      "taxamount": 0.13425, 
      "seatname": "Seat 1" 
     }, 
     { 
      "menuitemid": 44, 
      "qty": "1", 
      "taxids": [ 
       { 
        "taxid": "1", 
        "Amount": 0.13425 
       } 
      ], 
      "taxamount": 0.13425, 
      "seatname": "Seat 2" 
     }, 
     { 
      "menuitemid": 44, 
      "qty": "1", 
      "taxids": [ 
       { 
        "taxid": "1", 
        "Amount": 0.13425 
       } 
      ], 
      "taxamount": 0.13425, 
      "seatname": "Seat 2" 
     } 
    ], 
    "checkdiscountshiftlevelid": "", 
    "customerid": "0" 
} 

我嘗試了很多次,我沒有得到結果。我的示例代碼如下。

package servicecall; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class Savecheckandprint { 

    public void calll() throws JSONException 
    { 
     JSONObject obj1 = new JSONObject(); 
     try { 

      obj1.put("tableid", "41"); 
      obj1.put("status", "141"); 

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

     JSONObject products = new JSONObject(); 
     try { 
      products.put("menuitemid", "349"); 
      products.put("qty", "2"); 

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


     JSONArray jsonArray = new JSONArray(); 

     //jsonArray.put(obj1); 
     jsonArray.put(products); 



     JSONObject studentsObj = new JSONObject(); 
     studentsObj.put("", obj1); 
     studentsObj.put("", jsonArray); 



     String jsonStr = studentsObj.toString(); 

     System.out.println("jsonString: "+jsonStr); 
    } 
} 

剛到android。我不知道如何創建一個類似於JSON的結構。請幫幫我。請幫幫我。

+1

它真的很難理解你的要求,請帶有一點時間來糾正你的語法和廣告更多的細節 – erik

回答

1
package servicecall; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class Savecheckandprint { 

    public void calll() throws JSONException 
    { 
     JSONObject obj1 = new JSONObject(); 
     try { 

      obj1.put("tableid", "41"); 
      obj1.put("status", "141"); 

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

     JSONObject products = new JSONObject(); 
     try { 
      products.put("menuitemid", "349"); 
      products.put("qty", "2"); 

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


     JSONArray jsonArray = new JSONArray(); 
     jsonArray.put(products); 

     try { 

      obj1.put("products", jsonArray); 

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

     String jsonStr = obj1.toString(); 

     System.out.println("jsonString: "+jsonStr); 
    } 
} 
相關問題