如何在android中创建JSON格式?

时间:2014-07-14 08:55:22

标签: java android json android-json

我想创建一个json数据来解析服务为字符串,例如http://xyzabc.com/pos/getstatus?parse=mystring

该字符串必须包含以下数据。我在arraylist中将这些数据作为同名。

    {
    "ticket": "1-0006",
    "saleID": "140704131457005701",
    "tableid": 41,
    "status": 141,
    "total": "45.140",
    "layoutid": 2,
    "tablename": "66",
    "noofcustomer": 2,
    "userid": 1,
    "ordertype": 210,
    "istaxexcemt": 0,
    "gratuityid": "",
    "gratuitypercentage": "",
    "generatedon": "",
    "isprintonbill": "",
    "products": [
        {
            "menuitemid": 349,
            "qty": "1",
            "price": 2,
            "price2": 2,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.15
                }
            ],
            "taxamount": 0.15,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704131457011",
            "menuitemname": "Cherry Coke",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 44,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704131457012",
            "menuitemname": "Sweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 43,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704131457013",
            "menuitemname": "Unsweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 3,
            "qty": "1",
            "price": 8.95,
            "price2": 15.95,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.67125
                }
            ],
            "taxamount": 1.19625,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "8",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [
                {
                    "modifierid": "29",
                    "modifierqty": "1",
                    "modifierprice": "1",
                    "modifiergroupid": "24",
                    "modifierslice": "0",
                    "TaxAmount": "0.075",
                    "taxpercentage": "7.5",
                    "pizzasliceid": "0",
                    "ModPref": "Extra",
                    "ModPrefid": 4,
                    "modtaxLst": [
                        {
                            "taxid": "1",
                            "taxpercent": "7.5",
                            "Amount": 0.075
                        }
                    ]
                },
                {
                    "modifierid": "30",
                    "modifierqty": "1",
                    "modifierprice": "2.5",
                    "modifiergroupid": "24",
                    "modifierslice": "0",
                    "TaxAmount": "0.1875",
                    "taxpercentage": "7.5",
                    "pizzasliceid": "0",
                    "ModPref": "",
                    "ModPrefid": 0,
                    "modtaxLst": [
                        {
                            "taxid": "1",
                            "taxpercent": "7.5",
                            "Amount": 0.1875
                        }
                    ]
                },
                {
                    "modifierid": "35",
                    "modifierqty": "1",
                    "modifierprice": "0.5",
                    "modifiergroupid": "24",
                    "modifierslice": "0",
                    "TaxAmount": "0.0375",
                    "taxpercentage": "7.5",
                    "pizzasliceid": "0",
                    "ModPref": "",
                    "ModPrefid": 0,
                    "modtaxLst": [
                        {
                            "taxid": "1",
                            "taxpercent": "7.5",
                            "Amount": 0.0375
                        }
                    ]
                },
                {
                    "modifierid": "38",
                    "modifierqty": "1",
                    "modifierprice": "0.5",
                    "modifiergroupid": "24",
                    "modifierslice": "0",
                    "TaxAmount": "0.0375",
                    "taxpercentage": "7.5",
                    "pizzasliceid": "0",
                    "ModPref": "",
                    "ModPrefid": 0,
                    "modtaxLst": [
                        {
                            "taxid": "1",
                            "taxpercent": "7.5",
                            "Amount": 0.0375
                        }
                    ]
                },
                {
                    "modifierid": "43",
                    "modifierqty": "1",
                    "modifierprice": "2.5",
                    "modifiergroupid": "24",
                    "modifierslice": "0",
                    "TaxAmount": "0.1875",
                    "taxpercentage": "7.5",
                    "pizzasliceid": "0",
                    "ModPref": "",
                    "ModPrefid": 0,
                    "modtaxLst": [
                        {
                            "taxid": "1",
                            "taxpercent": "7.5",
                            "Amount": 0.1875
                        }
                    ]
                }
            ],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704131457014",
            "menuitemname": "Crab And Shrimp Dip",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 44,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704131517017",
            "menuitemname": "Sweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 41,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704142217017",
            "menuitemname": "Lemonade",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 244,
            "qty": "1",
            "price": 1.95,
            "price2": 1.95,
            "seatid": 1,
            "status": 139,
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 0,
            "taxids": [],
            "taxamount": 0.0001,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "not tastier",
            "coursingid": "24",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "201407041314570110",
            "menuitemname": "S. Bread",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 277,
            "qty": "1",
            "price": 8,
            "price2": 8,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 0,
            "taxids": [],
            "taxamount": 0.0001,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "24",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "201407041314570111",
            "menuitemname": "S. S.F. SAL",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 280,
            "qty": "1",
            "price": 1.95,
            "price2": 2.45,
            "seatid": 1,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 0,
            "taxids": [],
            "taxamount": 0.0001,
            "seatname": "Seat 1",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "24",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [
                {
                    "modifierid": "182",
                    "modifierqty": "1",
                    "modifierprice": "0.5",
                    "modifiergroupid": "31",
                    "modifierslice": "0",
                    "TaxAmount": "0",
                    "taxpercentage": "0",
                    "pizzasliceid": "0",
                    "ModPref": "Extra",
                    "ModPrefid": 4,
                    "modtaxLst": []
                }
            ],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "201407041314570112",
            "menuitemname": "S. Sd. Sal.",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 44,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 2,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 2",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "201407041422510110",
            "menuitemname": "Sweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 44,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 2,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 2",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704142251017",
            "menuitemname": "Sweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 44,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 2,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 2",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704142251018",
            "menuitemname": "Sweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        },
        {
            "menuitemid": 44,
            "qty": "1",
            "price": 1.79,
            "price2": 1.79,
            "seatid": 2,
            "status": "141",
            "holdtime": 0,
            "istaxexcemt": 0,
            "taxpercent": 7.5,
            "taxids": [
                {
                    "taxid": "1",
                    "taxpercent": "7.5",
                    "Amount": 0.13425
                }
            ],
            "taxamount": 0.13425,
            "seatname": "Seat 2",
            "shiftlevelid": "",
            "reason": "",
            "coursingid": "2",
            "istaxinclude": "0",
            "ordertype": "210",
            "modifiers": [],
            "saleid": "140704131457005701",
            "SaleDetailsMenuItem_ID": "20140704142251019",
            "menuitemname": "Sweet Tea",
            "timeeventid": "",
            "discountamount": "0",
            "definitiontype": "",
            "discountreason": "",
            "discountistaxadjust": 1,
            "discounttax": "0",
            "discountpercentage": 0,
            "discountshiftlevelid": ""
        }
    ],
    "checktimeeventid": "",
    "checkdiscountamount": "0",
    "checkdefinitiontype": "",
    "checkdiscountreason": "",
    "checkdiscountistaxadjust": 1,
    "checkdiscounttax": "0",
    "checkdiscountpercentage": 0,
    "checkdiscountshiftlevelid": "",
    "customerid": "0"
}

请有人帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

  1. 创建包含您发布的数据的对象。像这样:

    public class MyData {
      private String ticket;
      private long saleID;
      // ...
      private List<MyProduct> products;
      // constructor, getters and setters...
    }
    
    public class MyProduct {
      private int menuItemId;
      private int qty;
      // ...
    }
    
  2. 当您收到上面的JSON-Data-String时,您可以在GSON的帮助下将数据解析到您的对象中:

    import com.google.gson.Gson;
    
    final String jsonDataString = receive();
    final MyData myData = new Gson().fromJson(jsonDataString, MyData.class);
    
  3. 当您在对象中收集数据并希望发送JSON-Data-String时,您也可以使用GSON:

    final MyData myData = collectData();
    final String jsonDataString = new Gson().toJson(myData);
    send(jsonDataString);
    

答案 1 :(得分:0)

我建议你创建一个setter和getter类,它具有与上面提到的json字符串相同的变量名。还要在该类中使用关联(继承)。在json内容中,在每个开始和结束花括号下,所有值都类似于java类的字段。

创建整个setter和getter类后,使用setter为每个变量设置值。然后创建一个像JSONObject类的Object。

public class JsonSimpleExample {
 public static void main(String[] args) {

JSONObject obj = new JSONObject();
obj.put("name", "Sample");
obj.put("age", new Integer(100));

JSONArray list = new JSONArray();
list.add("msg 1");
list.add("msg 2");
list.add("msg 3");

obj.put("messages", list);

try {

    FileWriter file = new FileWriter("c:\\test.json");
    file.write(obj.toJSONString());
    file.flush();
    file.close();

} catch (IOException e) {
    e.printStackTrace();
}

System.out.print(obj);

 }

}