I am trying to parse JSON using a Notes agent, JSON is fetched using Apache HttpClient.
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);
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"
}
]
I have tried to use JSONObject
and JSONArray
from JSON.org but could not get it to work
I need some example code from the json.org package or other ways to parse the 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:
I saved the JSON in a JSONBlob at http://jsonblob.com/537a43bfe4b047fa2ef5f15d and created a unit test that requests that JSON:
And the output from running it is:
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).