28

我想从 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"
        }
    ]
}
4

5 回答 5

63

您可以将字符串转换为 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) {
  }
于 2012-05-29T18:17:16.510 回答
1

使用 gson 和 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();
}
于 2020-06-28T10:01:16.527 回答
0

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

于 2012-05-29T18:17:02.457 回答
0

尝试这个

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);
于 2012-05-29T18:22:01.583 回答
0
  public void responce() throws ClientProtocolException, IOException {
            org.apache.http.client.HttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost("https://www.example.com/api/authenticate");
    
            List<NameValuePair> params = new ArrayList<NameValuePair>(2);
            
            params.add(new BasicNameValuePair("UserName", "anything"));
            params.add(new BasicNameValuePair("Password", "usepassworld"));
            
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
    
            if (entity != null){
               String string_1 = EntityUtils.toString(entity); 
               System.out.println("the token we are generated is -->"+ string_1);   
                }
            }           
    }
于 2021-08-04T09:33:26.193 回答