2013-08-05 35 views
0

解析Android中的JSON字符串時遇到問題。字符串本身看起來很好,至少它看起來像我想要返回的東西。但是我試圖解析它時崩潰了。任何人都能看到爲什麼謝謝!解析Android中的JSON字符串時出錯

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

public JSONObject makeHttpRequest(String url, List<NameValuePair> params) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      if (params != null) { 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 
      } 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "n"); 
      } 
      is.close(); 
      json = sb.toString(); 
      Log.e("JSON", json); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      System.err.println("start try3"); //<--fine here 
      System.err.println(json);   //<--json string looks good 
      jObj = new JSONObject(json);  
      System.err.println("done try3"); //<--never outputs 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 

堆棧跟蹤:

08-05 15:28:00.665:E/JSON解析器(20009):錯誤解析數據 org.json.JSONException:值 [{ 「ID」 : 「3」, 「bool_gets_sms」: 「0」, 「picture_url」: 「無」, 「電子郵件」: 「[email protected]」, 「細胞」: 「12345」, 「名」: 「海瑟」}, { 「ID」: 「7」, 「bool_gets_sms」: 「0」, 「picture_url」: 「無」, 「電子郵件」: 「[email protected]」, 「細胞」: 「12335」, 「名」:」艾倫 「},{」 ID 「:」 10" , 「bool_gets_sms」: 「0」, 「picture_url」: 「無」, 「電子郵件」: 「[email protected]」, 「細胞」: 「12345」,」命名 「:」 珍妮 「},{」 ID 「:」 11" , 「bool_gets_sms」: 「0」, 「picture_url」: 「無」, 「電子郵件」: 「[email protected]」, 「細胞」:」 12345" , 「名」: 「傑夫」},{ 「ID」: 「24」,」 bool_gets_sms「:」0「,」picture_url「:」none「,」email「:」[email protected]「,」cell「:」12345「,」name「:」Rob「}] org.json類型.JSONArray無法轉換爲JSONObject

回答

0

您正在實例化爲JSONObject,即JSONArray,更改該數據類型和構造函數。

從您的堆棧跟蹤:

of type org.json.JSONArray cannot be converted to JSONObject 

所以,與其這樣:

static JSONObject jObj = null; 
// ... 
jObj = new JSONObject(json); 

此:

static JSONArray jArr = null; //changed jObj to jArr for your naming conventions too... 
// ... 
jArr = new JSONArray(json); 

你應該知道服務器將返回什麼類型的數據組織在提出請求之前,儘管可以通過編程方式查找,但最好事先知道。 JSON是非常簡單的,你可以在read through the spec ......幾分鐘TL; DR:大括號圍繞文本表示對象,方括號表示數組...

+0

我可以更改服務器返回的內容。更改爲JSONArray的問題很多,因爲它有許多下游的後果。你能建議服務器應該返回什麼類型的格式以適應JSONObject類嗎? – Alex

+0

@ usr55410您只需將該數組嵌套到對象中即可。你的服務器運行什麼語言? – Tonithy

+0

php。我的php代碼中的最後一行是'echo json_encode($ array);'。如果它能夠更加無縫地融入JSONObject類,我可以靈活地改變它。 – Alex

0

你應該那樣做:

JsonElement el = new JsonParser().parse(json); 
JSONObject obj= el.getAsJSONObject("the key you are looking for"); 

方法的確切名稱可能會有所不同,具體取決於您用於JSON解析的庫。 小心獲取JSONArray中的數組而不是JSONObject。