2011-10-18 162 views
0

我有一個Web服務逐字返回下面的字符串:Android的JSON解析煩惱

"{\"type\":\"youtube\", \"data\":\"http://66.84.12.156/android/?x=12&uid=4&lati=40.73972412&longi=-73.99234962&y=14&pixel_id=7224&pid=4&surface_id=7&fn=showHTML&data_id=7224&data=kT2UQ8TYMpk\",\"pixel_id\":\"471\",\"x\":\"12\",\"y\":\"14\",\"pid\":\"4\",\"surface_id\":\"7\",\"data_id\":\"7224\",\"user_id\":\"4\"}"

代碼看起來艾克:

dataScanner.client = new DefaultHttpClient(); 
dataScanner.post = new HttpPost("http://someurl/somepage.php"); 
post.setEntity(new UrlEncodedFormEntity(userKV)); 
Log.d("DST Scanner", "post string:" + post.toString()); 
HttpResponse response = client.execute(post); 

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
     StringBuilder builder = new StringBuilder(); 
     for (String line = null; (line = reader.readLine()) != null;) { 
      builder.append(line); 
     } 
     Log.d("DST Scanner", "Post Response (string)" + builder.toString()); 
     //JSONTokener tokener = new JSONTokener(builder.toString()); 
     finalResult = new JSONObject(builder.toString()); 

我已經嘗試了許多不同的格式(轉義引號,轉義報價,周圍沒有引號,躲過正斜槓),但我不斷收到此錯誤:

org.json.JSONException: Value {"type":"youtube", "data":"http://66.84.12.156/android/?x=12&uid=4&lati=40.73972412&longi=-73.99234962&y=14&pixel_id=7224&pid=4&surface_id=7&fn=showHTML&data_id=7224&data=kT2UQ8TYMpk","pixel_id":"471","x":"12","y":"14","pid":"4","surface_id":"7","data_id":"7224","user_id":"4"} of type java.lang.String cannot be converted to JSONObject

一切升ooks對我很好,但我一直在看這個這麼長時間,我不會感到驚訝,如果有一些愚蠢的事情,我正在做..

+0

的web服務被打破了。 json字符串中的引號不應該像這樣轉義。 –

回答

0

我是新來json,但所有的JSON開始和以「[」和「]」結尾,我使用PHP來回顯json_encode方法。

在Android側我使用:

try { 
     URL pHH = new URL("http://192.168.1.5/somephp.php"); 
     URLConnection WC = pHH.openConnection(); 
     BufferedReader in = new BufferedReader(new    InputStreamReader(WC.getInputStream())); 

     String line; 
     while((line = in.readLine()) != null){ 
      JSONArray ja = new JSONArray(line); 

      for (int i = 0; i < ja.length(); i++){ 
       JSONObject jo = (JSONObject) ja.get(i); 
       items[i] = jo.getString("title"); 
       thumbnails[i] = jo.getString("thumb"); 
       links[i] = jo.getString("link"); 
      } 
     } 
0
char[] utf8 = null; 
      StringBuilder properString = new StringBuilder(""); 

      utf8 = Response.toCharArray(); 

      for (int i = 0; i < utf8.length; i++) { 
       if ((int) utf8[i] < 65000) { 
        properString.append(utf8[i]); 
       } 
      } 
    System.out.println("Response of Login::" 
         + properString.toString());