【问题标题】:Parsing JSON from HttpClient request using JSON.org parser使用 JSON.org 解析器从 HttpClient 请求解析 JSON
【发布时间】:2014-07-07 17:53:15
【问题描述】:

我正在尝试使用 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 的方法。

【问题讨论】:

  • 当您解析 JSON 和 toString 结果时,它应该看起来与传入的 JSON 非常相似。解析时会得到什么? (你有没有去 json.org 学过 JSON 语法?只需要 5-10 分钟就能学会。)
  • @Thomas:你的解决方案对我来说是完美的。您使用哪个版本的 apache http 客户端?我正在使用多米诺服务器 9。

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


【解决方案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 类将 InputStream 从 HttpResponse 实体转换,但这可以按照你喜欢的方式完成(并且像我一样转换它可能不是最好的主意,具体取决于 JSON 的大小)。

【讨论】:

猜你喜欢
  • 2019-05-03
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2014-03-22
相关资源
最近更新 更多