问题描述
我正在尝试使用Notes代理解析JSON,并使用Apache HttpClient来获取JSON.
I am trying to parse JSON using a Notes agent, JSON is fetched using Apache HttpClient.
这是返回JSON的代码
Here is the code that return the 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看起来像这样.
The JSON Looks like this.
[
{
"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中的JSONObject
和JSONArray
,但无法使其正常工作我需要json.org包中的示例代码或其他解析json的方法.
I have tried to use JSONObject
and JSONArray
from JSON.org but could not get it to workI need some example code from the json.org package or other ways to parse the json.
推荐答案
您可以使用 HttpResponse#getEntity .一旦有了,就可以创建一个新的JSONArray并迭代该数组以访问JSON对象中的值:
You can get the JSON from the Entity in the HttpResponse using HttpResponse#getEntity. Once you have that, then just create a new JSONArray and iterate the array to access the values in your JSON object:
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:
I saved the JSON in a JSONBlob at http://jsonblob.com/537a43bfe4b047fa2ef5f15d and created a unit test that requests that 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的大小,像我一样转换它并不是最佳方法).
I used the IOUtils class to convert the InputStream from the HttpResponse Entity, but this can be done anyway you like (and converting it like I did may not be the best idea depending on how big the JSON is).
这篇关于使用JSON.org解析器从HttpClient请求解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!