2014-05-23 187 views
0

我有一個字符串,如下面,我從網絡服務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字符串

的可能有人能幫助我在這個問題上。我無法弄清楚我自己過去三天:(

+0

爲什麼你不使用org.json內置android –

+0

我正在使用Azure移動服務。 GSON與它密切配合。 – user3296042

+0

Gson基本上是設計來轉換POJO類中的json響應。否則org.json是好的 –

回答

1

做這樣

您可以輕鬆地解析JSON使用org.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) { 
     } 
+0

謝謝你工作:) – user3296042

-1

您可以使用此方法

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; 
} 
+0

只發布您的代碼的相關部分。 –

0

嘗試把.replaceAll("\\\","")在你的代碼只要你代碼解析您的JSON。它的選擇。 :))