Java JSON数组字符串到JSON数组
我从web服务收到一个字符串,如下所示Java JSON数组字符串到JSON数组,java,android,json,exception,gson,Java,Android,Json,Exception,Gson,我从web服务收到一个字符串,如下所示 [ { "id": "AEC56A1C-E590-4CCE-B3DD-7AFF8FEE9216", "itemname": "tyuytuytuytuytutyu", "itemdescription": "hdfg dag dg sd gsg sdg sdg sg sgd", "itemprice": 40, "specialprice": 0, "i
[
{
"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对象错误
这是一个堆栈跟踪
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\\\
strace的其余部分充满了json字符串
有人能帮我解决这个问题吗。在过去的3天里,我自己都无法理解:(您可以使用此方法解析json
public static ArrayList parseJSON(String url, int method,List<NameValuePair> params, Class classType) {
ArrayList list = new ArrayList();
StrictMode.enableDefaults();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
String respo = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = null;
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
StatusLine statusLine = httpResponse.getStatusLine();
int statuscode = statusLine.getStatusCode();
Log.v("Pre School App", "Response code = " + statuscode);
if (statuscode == 200) {
HttpEntity entity = httpResponse.getEntity();
respo = EntityUtils.toString(entity);
Log.e("Pre School App test ", "Status code = " + respo);
} else {
Log.e("Pre School App test ", "Status code = " + respo);
}
// HttpEntity entity = httpResponse.getEntity();
// respo = EntityUtils.toString(entity);
} catch (Exception e) {
// Toast.makeText(myContext, "Error !" + e,
// Toast.LENGTH_LONG).show();
e.printStackTrace();
}
if (classType != null) {
try {
JSONArray jArray = new JSONArray(respo);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
Gson gson = new Gson();
list.add(gson.fromJson(json.toString(), classType));
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
return null;
}
return list;
}
公共静态ArrayList parseJSON(字符串url、int方法、列表参数、类类型){
ArrayList=新建ArrayList();
StrictMode.enableDefaults();
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
字符串respo=“”;
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpResponse HttpResponse=null;
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
//添加post参数
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
//将参数附加到url
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
StatusLine StatusLine=httpResponse.getStatusLine();
int statuscode=statusLine.getStatusCode();
Log.v(“学前应用程序”、“响应代码=“+statuscode”);
如果(状态代码==200){
HttpEntity entity=httpResponse.getEntity();
respo=EntityUtils.toString(实体);
Log.e(“学前应用程序测试”,“状态代码=”+respo);
}否则{
Log.e(“学前应用程序测试”,“状态代码=”+respo);
}
//HttpEntity entity=httpResponse.getEntity();
//respo=EntityUtils.toString(实体);
}捕获(例外e){
//Toast.makeText(myContext,“Error!”+e,
//Toast.LENGTH_LONG).show();
e、 printStackTrace();
}
if(类类型!=null){
试一试{
JSONArray jArray=新JSONArray(respo);
for(int i=0;i
尝试将.replaceAll(“\\\”,“)
放在代码中的任何位置。这是您的选择。:)这样做
您可以使用org.json轻松解析json
try {
JSONArray jArray = new JSONArray(responseString);
int size= jArray.length();
for (int i = 0; i < size; i++) {
JSONObject item= jArray.getJSONObject(i);
System.out.println("id : " + item.getString("id"));
System.out.println("itemname : " + item.getString("itemname"));
System.out.println("itemdescription : " + item.getString("itemdescription"));
// ........ so on
}
} catch (Exception e) {
}
试试看{
JSONArray jArray=新JSONArray(responseString);
int size=jArray.length();
对于(int i=0;i
您为什么不使用Android内置的org.json呢?我正在使用Azure移动服务。GSON基本上是为了在POJO类中转换json响应而设计的。否则org.json是ok@BirajZalavadia你认为我可以使用json提取数据吗?