Java – use JSON Org parser parses JSON from httpclient request

I tried to parse JSON using notes proxy and get JSON using Apache httpclient

This is the code that returns 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 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 tried to use JSON Jsonobject and jsonarray in. Org, but they can't work. I need some from JSON Org package or other methods to parse JSON

Solution

You can use httpresponse #getentity to get JSON from the entity in httpresponse Once you have it, you can create a new jsonarray and iterate over the array to access the values in the 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"));
}

I am here http://jsonblob.com/537a43bfe4b047fa2ef5f15d Save JSON in jsonblob and create a unit test requesting 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"));
        }
    }
}

The output of running it is:

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

I use the ioutils class to convert the InputStream from the httpresponse entity, but you can do so anyway (and converting it like me may not be the best idea, depending on the size of JSON)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>