Java 如何在android中创建JSON格式?

Java 如何在android中创建JSON格式?,java,android,json,android-json,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", "layo

我想创建一个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"
}
请任何人帮我解决这个问题

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

    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;
      // ...
    }
    
  • 在对象中收集数据并希望发送JSON数据字符串时,也可以使用GSON:

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

  • 我建议您创建一个setter和getter类,其变量名与前面提到的json字符串中的变量名相同。在该类中也使用关联(继承)。在json内容中,在每个开头和结尾的花括号下,所有值都类似于java类的字段

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

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

    }

    您能澄清一下您到底想做什么吗?您所要做的就是将该字符串转换为json对象?我必须将其解析为服务。我无法从服务获取此数据。对不起,我不明白您的意思。如果您正在接收上面粘贴的数据,请执行步骤1和2;如果必须创建上面粘贴的数据,请执行步骤1和3。
    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);
    
     }