我有一個字符串,如下面,我從網絡服務JSON字符串數組到JSON數組
[
{
"id": "AEC56A1C-E590-4CCE-B3DD-7AFF8FEE9216",
"itemname": "tyuytuytuytuytutyu",
"itemdescription": "hdfg dag dg sd gsg sdg sdg sg sgd",
"itemprice": 40,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "6A1A4E13-6AFD-44DD-998D-3A51E35C2C23",
"status": true,
"itemlongname": "yutyuyut",
"iscombodeal": false
},
{
"id": "E29249B2-E620-487C-9D66-1BC07A1B1D93",
"itemname": "rrrr",
"itemdescription": "rrrrr",
"itemprice": 20,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "6A1A4E13-6AFD-44DD-998D-3A51E35C2C23",
"status": true,
"itemlongname": "rrrr",
"iscombodeal": false
},
{
"id": "D273D888-0526-4487-8113-6405C8E216D5",
"itemname": "vTD",
"itemdescription": "vTDvTD",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "6A1A4E13-6AFD-44DD-998D-3A51E35C2C23",
"status": true,
"itemlongname": "vTD",
"iscombodeal": false
},
{
"id": "DF6DC5E3-25E9-4D57-89F4-C2BA8A719866",
"itemname": "NO option",
"itemdescription": "NO option",
"itemprice": 45.78,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "6A1A4E13-6AFD-44DD-998D-3A51E35C2C23",
"status": true,
"itemlongname": "NO option",
"iscombodeal": false
},
{
"id": "AD1739A6-B50A-4780-B71E-9E4E98AF19B5",
"itemname": "ttttt",
"itemdescription": "tttt",
"itemprice": 12,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "6A1A4E13-6AFD-44DD-998D-3A51E35C2C23",
"status": true,
"itemlongname": "tttttt",
"iscombodeal": false
},
{
"id": "C3206E79-BD92-4F49-B291-C57EB36AFCCC",
"itemname": "Marinara (Seafood)",
"itemdescription": "(Anchovies optional), shrimps, crab, mussels, squids, mozzarella",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Marinara (Seafood)",
"iscombodeal": false
},
{
"id": "FD3603B9-1175-463F-A6AD-CCF1141E135D",
"itemname": "Hawaiian",
"itemdescription": "Double ham, double pineapple, mozzarella",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Hawaiian",
"iscombodeal": false
},
{
"id": "9C34BAA6-85ED-4E49-A0A5-B99EF88472EC",
"itemname": "Siliciana",
"itemdescription": "Salami, onions, capsicum, mushroom, tomato, mozzarella",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Siliciana (Olives optional)",
"iscombodeal": false
},
{
"id": "4E024FDF-46F1-4E91-A26F-468E3C7804A0",
"itemname": "Hot Hot",
"itemdescription": "Salami, onion, fresh garlic, chilli, parmesan cheese, mozzarella and jalapeno",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Hot Hot",
"iscombodeal": false
},
{
"id": "C0EC7736-3AB1-4D52-AE47-97D01BEA8A9C",
"itemname": "Meat Treat",
"itemdescription": "(BBQ sauce optional) ham, salami, bacon, cabanossi, mozzarella",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Meat Treat",
"iscombodeal": false
},
{
"id": "0B6E4A06-9865-4A88-AAFC-1C45F688B4C9",
"itemname": "Pizza to Go Special",
"itemdescription": "Ham, salami, onion, pineapple, mushrooms, capsicum, mozzarella",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Pizza to Go Special",
"iscombodeal": false
},
{
"id": "E54F3F0B-7E07-4D6A-BAE4-6C8B8B87210D",
"itemname": "Capriciosa",
"itemdescription": "salami, mushroom, tomato and mozzarella",
"itemprice": 0,
"specialprice": 0,
"itemimageurl": "no-url",
"catid": "9960CA1F-46D3-4532-95E7-84F75B3771FE",
"status": true,
"itemlongname": "Capriciosa",
"iscombodeal": false
}
]
收到現在,我想將其更改爲jArray(GSON libraray)。變量itemsJsonString
包含通過Web服務接收到的字符串。
JsonArray jArray = new JsonArray();
JsonPrimitive element = new JsonPrimitive(gson.toJson(itemsJsonString));
jArray.add(element);
當我運行上面的代碼時,我得到Illegalstateexception不是json對象錯誤。
這是一個堆棧跟蹤。提供與strace的
05-23 12:27:14.443: E/AndroidRuntime(26697): FATAL EXCEPTION: main
05-23 12:27:14.443: E/AndroidRuntime(26697): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.proitzen.pizzatogo/com.proitzen.pizzatogo.ItemActivity}:
java.lang.IllegalStateException: Not a JSON Object: "\"[{\\\"id\\\":\\\"AEC56A1C-E590-4CCE-B3DD-7AFF8FEE9216\\\",\\\"itemname\\\":\\\"tyuytuytuytuytutyu\\\",\\\"itemdescription\\\":\\\"hdfg dag dg sd gsg sdg sdg sg sgd\\\",\\\"itemprice\\\":40,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"6A1A4E13-6AFD-44DD-998D-3A51E35C2C23\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"yutyuyut\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"E29249B2-E620-487C-9D66-1BC07A1B1D93\\\",\\\"itemname\\\":\\\"rrrr\\\",\\\"itemdescription\\\":\\\"rrrrr\\\",\\\"itemprice\\\":20,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"6A1A4E13-6AFD-44DD-998D-3A51E35C2C23\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"rrrr\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"D273D888-0526-4487-8113-6405C8E216D5\\\",\\\"itemname\\\":\\\"vTD\\\",\\\"itemdescription\\\":\\\"vTDvTD\\\",\\\"itemprice\\\":0,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"6A1A4E13-6AFD-44DD-998D-3A51E35C2C23\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"vTD\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"DF6DC5E3-25E9-4D57-89F4-C2BA8A719866\\\",\\\"itemname\\\":\\\"NO option\\\",\\\"itemdescription\\\":\\\"NO option\\\",\\\"itemprice\\\":45.78,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"6A1A4E13-6AFD-44DD-998D-3A51E35C2C23\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"NO option\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"AD1739A6-B50A-4780-B71E-9E4E98AF19B5\\\",\\\"itemname\\\":\\\"ttttt\\\",\\\"itemdescription\\\":\\\"tttt\\\",\\\"itemprice\\\":12,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"6A1A4E13-6AFD-44DD-998D-3A51E35C2C23\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"tttttt\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"C3206E79-BD92-4F49-B291-C57EB36AFCCC\\\",\\\"itemname\\\":\\\"Marinara (Seafood)\\\",\\\"itemdescription\\\":\\\"(Anchovies optional), shrimps, crab, mussels, squids, mozzarella\\\",\\\"itemprice\\\":0,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"9960CA1F-46D3-4532-95E7-84F75B3771FE\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"Marinara (Seafood)\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"FD3603B9-1175-463F-A6AD-CCF1141E135D\\\",\\\"itemname\\\":\\\"Hawaiian\\\",\\\"itemdescription\\\":\\\"Double ham, double pineapple, mozzarella\\\",\\\"itemprice\\\":0,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"9960CA1F-46D3-4532-95E7-84F75B3771FE\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"Hawaiian\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"9C34BAA6-85ED-4E49-A0A5-B99EF88472EC\\\",\\\"itemname\\\":\\\"Siliciana\\\",\\\"itemdescription\\\":\\\"Salami, onions, capsicum, mushroom, tomato, mozzarella\\\",\\\"itemprice\\\":0,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"9960CA1F-46D3-4532-95E7-84F75B3771FE\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"Siliciana (Olives optional)\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"4E024FDF-46F1-4E91-A26F-468E3C7804A0\\\",\\\"itemname\\\":\\\"Hot Hot\\\",\\\"itemdescription\\\":\\\"Salami, onion, fresh garlic, chilli, parmesan cheese, mozzarella and jalapeno\\\",\\\"itemprice\\\":0,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"9960CA1F-46D3-4532-95E7-84F75B3771FE\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"Hot Hot\\\",\\\"iscombodeal\\\":false},{\\\"id\\\":\\\"C0EC7736-3AB1-4D52-AE47-97D01BEA8A9C\\\",\\\"itemname\\\":\\\"Meat Treat\\\",\\\"itemdescription\\\":\\\"(BBQ sauce optional) ham, salami, bacon, cabanossi, mozzarella\\\",\\\"itemprice\\\":0,\\\"specialprice\\\":0,\\\"itemimageurl\\\":\\\"no-url\\\",\\\"catid\\\":\\\"9960CA1F-46D3-4532-95E7-84F75B3771FE\\\",\\\"status\\\":true,\\\"itemlongname\\\":\\\"Meat Treat\\\",\\\"iscombodeal\\\
剩下的就是充滿了JSON字符串
的可能有人能幫助我在這個問題上。我無法弄清楚我自己過去三天:(
爲什麼你不使用org.json內置android –
我正在使用Azure移動服務。 GSON與它密切配合。 – user3296042
Gson基本上是設計來轉換POJO類中的json響應。否則org.json是好的 –