我想从 Web 服务中检索 JSON 并对其进行解析。
我走对了吗?
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"
}
]
}
最佳答案
您可以将字符串转换为 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) {
}
关于android - 如何将 HttpEntity 转换为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10804466/