2013-05-03 225 views
0

即時通訊嘗試解析使用android發送php的json數據。這是代碼。 JSON的輸出看起來是這樣的: 「{文件:[{IDNumber中: '28044684',DOCTYPE: '身份證'}],成功:1}」JSON字符串解析

即時得到錯誤ERROR parsing JSONObject value [null];甚至當我直接分配json輸出到變量json。可能是什麼問題呢??

String json = ""; 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("idnumber", parameter)); 
     // Making HTTP request 
     try { 

      // check for request method 
      if (method == "POST") { 
       // request method is POST 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      } else if (method == "GET") { 
       // request method is GET 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 
       HttpResponse httpResponse = httpClient.execute(httpGet); 
       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, "utf-8"), 300); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line=reader.readLine())!= null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 
     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (Exception e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      try { 
       jObj = new JSONObject(json.substring(json.indexOf("{"), 
         json.lastIndexOf("}") + 1)); 
      } catch (Exception e0) { 
       Log.e("JSON Parser0", 
         "Error parsing data [" + e0.getMessage() + "] " 
           + json); 
       Log.e("JSON Parser0", "Error parsing data " + e0.toString()); 
       try { 
        jObj = new JSONObject(json.substring(1)); 
       } catch (Exception e1) { 
        Log.e("JSON Parser1", 
          "Error parsing data [" + e1.getMessage() + "] " 
            + json); 
        Log.e("JSON Parser1", 
          "Error parsing data " + e1.toString()); 
        try { 
         jObj = new JSONObject(json.substring(2)); 
        } catch (Exception e2) { 
         Log.e("JSON Parser2", 
           "Error parsing data [" + e2.getMessage() 
             + "] " + json); 
         Log.e("JSON Parser2", 
           "Error parsing data " + e2.toString()); 
         try { 
          jObj = new JSONObject(json.substring(3)); 
         } catch (Exception e3) { 
          Log.e("JSON Parser3", "Error parsing data [" 
            + e3.getMessage() + "] " + json); 
          Log.e("JSON Parser3", "Error parsing data " 
            + e3.toString()); 
         } 
        } 
       } 
      } 
     } 
     return null; 
+0

json解析是非常簡單,但不是你做了什麼。嘗試一些教程 – 2013-05-03 10:20:43

+0

你必須更加具體,它可以是任何東西,即使是「返回null」。嘗試將其縮減爲可能的最簡單示例,使用硬編碼字符串並且不存在回退。 – Szocske 2013-05-03 10:22:50

回答

3

JSON無效

{ 
documents: [ 
    { 
     idnumber: '28044684', 
     doctype: 'NationalID' 
    } 
], 
success: 1 
} 

參見:http://json.org/example.html一些正確的格式

正確的格式應該是

{ 
"documents": [ 
    { 
     "idnumber": "28044684", 
     "doctype": "NationalID" 
    } 
], 
"success": 1 
} 

Validate這裏

+1

在JSON中,所有引號都是雙引號,所以''NationalID''而不是''NationalID'',並且所有的鍵都必須被引用,所以''文檔':'而不是'文檔:'' – johusman 2013-05-03 10:22:27

2

嘗試後捕捉的改變你的try塊這個

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(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

,然後提取您直接從JSONObject的要返回的信息

JSONObject json = jsonParser.makeHttpRequest(host + url_app_list, 
        "GET", params); 

      if (json == null) { 
       Log.e("Server Warning", "No Server Response"); 
       message = "No Server Response"; 
      } else { 

       Log.d("All Apps: ", json.toString()); 

       try { 

        success = json.getInt(TAG_SUCCESS); 

        if (success == 1) { 

         apps = json.getJSONArray(TAG_APPS); 

         for (int i = 0; i < apps.length(); i++) { 
          JSONObject c = apps.getJSONObject(i); 
          AppData tempApp = new AppData(); 

          tempApp.setName(c.getString(TAG_NAME)); 
          // tempApp.setPackageName(c.getString(TAG_PACKAGE)); 
          tempApp.setServerVersion(c.getInt(TAG_VERSION)); 
          tempApp.setServerDateVersion(c.getInt(TAG_DVERSION)) 

...等。這將使它更容易處理,但你的JSON是無效的,字符串應該全部用雙引號,看下面

{"success":0,"message":"Required field(s) missing"}