java - 使用 JSON.org 解析器从 HttpClient 请求中解析 JSON

标签 java json parsing xpages apache-httpclient-4.x

我正在尝试使用 Notes 代理解析 JSON,JSON 是使用 Apache HttpClient 获取的。

这是返回 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.org 的 JSONObjectJSONArray 但无法正常工作 我需要 json.org 包中的一些示例代码或其他解析 json 的方法。

最佳答案

您可以使用 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 的大小)。

关于java - 使用 JSON.org 解析器从 HttpClient 请求中解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23743370/

相关文章:

java - 无法安装 GWT 开发人员插件

java - 带有非最终变量的 lambda

json - 如何使用 Python 解析 SQS JSON 消息

parsing - 如何获取导致解析错误的字符串?

Java - 欧几里得算法的递归函数

java - 使用包含列表的流过滤数据?

javascript - 如何在 jquery success 方法中接收 json 并使用该 json 构建 html 内容

C# POST 到 OAUTH2 与 formdata

json - 获取此输出 json 的所有值

android - 在 android 中解析 KSoap2 响应