0

I have the following code for calling an API that returns JSON with the following format:-

{
  "data": [
    {
      " Ser": 1,
      " No": 1
    },
    {
      " Ser": 2,
      " No": 2
    },
    {
      " Ser": 3,
      " No": 3
    },
    {
      " Ser": 4,
      " No": 4
    },
    {
      " Ser": 5,
      " No": 5
    },

  ]
}

The code is:-

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("/json/api/getinfo?type=100");
    HttpResponse response = client.execute(request);
// Get the response, how i can loop through the returned JSON and assign the reterned json to a global parameters which i can access from ym system using has values #parameter1# , #parameter2#, etc.

so how can I loop through the returned JSON and assign it 'NO' to the global parameters? Best Regards

1 Answer 1

1

You should get the content out of the HttpReponse object through the HttpEntity.getContent() as mentioned in the documentation. The content should then be fed to a JSONObject from any JSON library like the one from json.org/java. Then you can traverse the JSON output through the JSONObject and extract elements out of it.

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) 
{
    //
    // Using Commons IO library's IOUtils method 
    // to read the content from the stream.
    //
    String json = IOUtils.toString(entity.getContent());
    JSONObject obj = new JSONObject(json);
    // Process the JSON

    // shutdown the connection.
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply, but is there an example or tutorial that can help me .
The example from HttpClient documentation page will help you get the JSON content. Then, based on the JSON library you decide to use, it would have sample examples to convert a String to JSONObject and how traverse it. So, I am not sure if there would be a single tutorial/example covering both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.