How to convert HttpEntity into JSON?

后端 未结 4 785
既然无缘
既然无缘 2020-12-09 08:44

I want to retrieve JSON from a web-service and parse it then.
Am I on the right way?

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet htt         


        
4条回答
  •  我在风中等你
    2020-12-09 08:50

    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) {
      }
    

提交回复
热议问题