How to convert HttpEntity into JSON?
Using gson and EntityUtils:
HttpEntity responseEntity = response.getEntity();
try {
if (responseEntity != null) {
String responseString = EntityUtils.toString(responseEntity);
JsonObject jsonResp = new Gson().fromJson(responseString, JsonObject.class); // String to JSONObject
if (jsonResp.has("property"))
System.out.println(jsonResp.get("property").toString().replace("\"", ""))); // toString returns quoted values!
} catch (Exception e) {
e.printStackTrace();
}
You can convert string to json as:
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) {
}