2015-12-15 22 views
0

我编写了RestClient来调用其余服务,然后解析响应。我用这个:由Rest客户端解析JSonObject响应java

HttpGet httpGet = new HttpGet(url); 
HttpResponse httpResponse = httpClient.execute(httpGet); 
HttpEntity httpEntity = httpResponse.getEntity(); 
is = httpEntity.getContent(); 

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) { 
} 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
    } 
    jObj = new JSONObject(json); 
    return jObj; 

我m到处回应是这样的:

{\"r\":{\"@status\":\"ok\",\"@st\":\"ok\",\"@call_id\":\"8410e\",\"method\":\"notification\",\"notification\":" + 
      "{\"@status\":\"true\",\"n\":{\"objects\":{\"@totalClassCount\":\"35\",\"@totalTestCount\":\"0\",\"@totalContentCount\":\"0\"," + 
      "\"@totalAssignmentCount\":\"5\",\"@totalOverdueCount\":\"16\",\"@totalCourseCount\":\"97\",\"@lastClassTime\":\"01-01-1753 12:00:00\"," + 
      "\"@lastTestTime\":\"01-01-1753 08:00:00\",\"@lastContentTime\":\"09-01-2015 02:52:30\",\"@lastAssgnTime\":\"01-01-1753 08:00:00\"," + 
      "\"@lastOverdueTime\":\"01-01-1753 12:00:00\",\"@lastCourseTime\":\"09-01-2015 02:52:30\",\"@lastCommentTime\":\"14-12-2015 03:48:55\"," + 
      "\"flag\":\"1\",\"object\":[{\"@type\":\"3\",\"content\":{\"@contentid\":\"137\",\"ntype\":\"1\",\"sime\":\"9 months ago\"," + 
      "\"title\":{\"#cdata-section\":\"xlsx icon file\"},\"courseid\":\"95137\",\"sid\":\"1976\",\"subid\":\"0\",\"coursetype\":\"1\",\"contentsize\":" + 
      "\"0\",\"csid\":\"2\",\"contenttypeid\":\"6\",\"contentviews\":\"0\",\"totalslides\":\"16\",\"iscreator\":\"0\"}}}]}}}}} 

能否请你帮我,我怎么能解析吗?

感谢, ITIN

回答

0

转换到JSON的代码是不完全正确的 - 尝试的JSONObject。你可以做得更好,如下所示。

if (httpEntity != null) { 
      String resultString = EntityUtils.toString(httpEntity); 
      // parsing JSON and convert String to JSON Object 
      JSONObject result = new JSONObject(resultString); 
    return result; 
+0

你意味着不是BufferedReader中读者=新的BufferedReader(新的InputStreamReader这个代码我使用的线路游码RT? – itin

+0

是,然后返回jObj。只需添加一个日志或系统输出返回之前看到的是什么。返回请接受并给予好评,如果这有助于 –

+0

如果(httpEntity!= NULL){ 字符串resultString = EntityUtils.toString(httpEntity)我已经这样做了。 //解析JSON和字符串转换为JSON对象 的JSONObject jObj =新JSONObject(resultString); } return jObj,Result has string,but jObj is null。 – itin