如何在android中以我们自己的格式创建json格式?

时间:2014-07-14 13:49:37

标签: java json jsonobject

我想创建一个JSON格式发送给服务。我在下面提到的json格式。 产品对象来自循环。请假设我= 1并给我一个答案。

{
    "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结构。请帮我。请帮帮我。

2 个答案:

答案 0 :(得分:1)

我不确定我理解您的问题,但您应该考虑使用GSON,它允许从对象/类创建简单的JSON

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

答案 1 :(得分: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);
    }
}