如何将HttpEntity转换为JSON?

我想从Web服务中检索JSON,然后parsing它。
我是否正确?

HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response; try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { // parsing JSON } } catch (Exception e) { } 

不幸的是我不知道如何将HttpEntity转换成JSONObject。

这是我的JSON(摘录):

 { "names": [ { "name": "Zachary" }, { "name": "Wyatt" }, { "name": "William" } ] } 

你可以将string转换为json,如下所示:

 try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { String retSrc = EntityUtils.toString(entity); // parsing JSON JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object JSONArray tokenList = result.getJSONArray("names"); JSONObject oj = tokenList.getJSONObject(0); String token = oj.getString("name"); } } catch (Exception e) { } 

使用entity.getContent()获取InputStream并将其转换为String。

尝试这个

 public String getMyFeed(){ HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclien.execute(httpget); HttpEntity entity = response.getEntity(); HttpInputStream content = entity.getContent(); StatusLine sl = response.getStatusLine(); int statCode = sl.getStatusCode() if (statCode ==200){ // process it } } String readFeed = getMyFeed(); JSONArray jArr = new JSONArray(readFeed); for(int i=0 ; i<jArr.length ; i++) JSONObject jObj = jArr.getJSONObject(i);