有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用JSON从HttpClient请求解析JSON。组织分析器

我试图使用Notes代理解析JSON,JSON是使用ApacheHttpClient获取的

下面是返回JSON的代码

import lotus.domino.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();

      HttpClient client = HttpClientBuilder.create().build();
      HttpGet request = new HttpGet("http://api.acme.com/customer");
      request.addHeader("accept", "application/json");
      request.addHeader("Host", "api.acme.com");
      request.addHeader("X-Api-Version", "1.0");
      request.addHeader("Authorization", "Basic ...");

      HttpResponse response = client.execute(request);       

JSON看起来像这样

[ 
  { 
    "id": 123456, 
    "insertDate": "2014-05-12T16:51:38.343", 
    "read": false, 
    "site": "acme.com", 
    "Email": "john.doe@acme.com", 
    "location": "/customer/1212?v=1.0" 
  } 
] 

我尝试使用JSON中的JSONObjectJSONArray。但无法使其工作 我需要一些来自json的示例代码。org包或其他解析json的方法


共 (1) 个答案

  1. # 1 楼答案

    您可以使用HttpResponse#getEntity从HttpResponse中的实体获取JSON。一旦有了这些,只需创建一个新的JSONArray并迭代该数组即可访问JSON对象中的值:

    String json = IOUtils.toString(response.getEntity().getContent());
    JSONArray array = new JSONArray(json);
    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        log.info("the id is {}", object.getInt("id"));
        log.info("the insertDate is {}", object.getString("insertDate"));
        log.info("read is {}", object.getBoolean("read"));
        log.info("the site is {}", object.getString("site"));
        log.info("the Email is {}", object.getString("Email"));
        log.info("the location is {}", object.getString("location"));
    }
    

    我将JSON保存在位于http://jsonblob.com/537a43bfe4b047fa2ef5f15d的JSONBlob中,并创建了一个单元测试来请求该JSON:

    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.io.IOUtils;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.json.JSONArray;
    import org.json.JSONObject;
    import org.junit.Test;
    
    @Slf4j
    public class JsonTest {
        @Test
        public void test() throws Exception {
            HttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet("http://jsonblob.com/api/jsonBlob/537a43bfe4b047fa2ef5f15d");
            request.addHeader("accept", "application/json");
            HttpResponse response = client.execute(request);
            String json = IOUtils.toString(response.getEntity().getContent());
            JSONArray array = new JSONArray(json);
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                log.info("the id is {}", object.getInt("id"));
                log.info("the insertDate is {}", object.getString("insertDate"));
                log.info("read is {}", object.getBoolean("read"));
                log.info("the site is {}", object.getString("site"));
                log.info("the Email is {}", object.getString("Email"));
                log.info("the location is {}", object.getString("location"));
            }
        }
    }
    

    运行它的输出是:

    11:23:19.508 [main] INFO  JsonTest - the id is 123456
    11:23:19.516 [main] INFO  JsonTest - the insertDate is 2014-05-12T16:51:38.343
    11:23:19.516 [main] INFO  JsonTest - read is false
    11:23:19.516 [main] INFO  JsonTest - the site is acme.com
    11:23:19.516 [main] INFO  JsonTest - the Email is john.doe@acme.com
    11:23:19.516 [main] INFO  JsonTest - the location is /customer/1212?v=1.0
    

    我使用了IOUtils类来转换来自HttpResponse实体的InputStream,但是这可以按照您喜欢的任何方式来完成(根据JSON的大小,像我这样进行转换可能不是最好的主意)