Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a JSON from HTTP Response in Java

Hi I am using Client Http (apache), and json-simple.

I want to access the attributes of the json response, and then use them.

Any idea how to do this? I read a post and did not work as it but me.

This is my answer json:

{"Name":"myname","Lastname":"mylastname","Age":19}

This is my code java:

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpGet getRequest = new HttpGet(
    "http://localhost:8000/responsejava");
getRequest.addHeader("accept", "application/json");

HttpResponse response = httpClient.execute(getRequest);

if (response.getStatusLine().getStatusCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
             + response.getStatusLine().getStatusCode());
}

BufferedReader br = new BufferedReader(
    new InputStreamReader( 
        (response.getEntity().getContent())
    )
);

StringBuilder content = new StringBuilder();
String line;
while (null != (line = br.readLine())) {
    content.append(line);
}

Object obj=JSONValue.parse(content.toString());
JSONObject finalResult=(JSONObject)obj;
System.out.println(finalResult);

httpClient.getConnectionManager().shutdown();

I printed null, What am I doing wrong?

like image 936
jojemapa Avatar asked Feb 21 '16 00:02

jojemapa


People also ask

How extract JSON data from string in Java?

JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser. parse(yourString); String msgType = (String) jsonObject. get("MsgType");

How do I get response JSON?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

What is parse JSON in Java?

JSON stands for JavaScript Object Notation, and it is based on a subset of JavaScript. As a data-exchange format, it is widely used in web programming. Here we show how to parse JSON in Java using the org. json library. A JSON object is an unordered set of key/value pairs.

How do I parse JSON in Rest assured?

We can parse JSON Response with Rest Assured. To parse a JSON body, we shall use the JSONPath class and utilize the methods of this class to obtain the value of a specific attribute. We shall first send a GET request via Postman on a mock API URL and observe the Response body.


2 Answers

Better and easier to use Gson

Gson gson = new Gson;
NameBean name = gson.fromJson(content.toString(),NameBean.class)

NameBean is the object where you persist the json string.

public class NameBean implements Serializable{
public String name;
public String lastname;
public Int age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public Int getAge() {
    return age;
}

public void setAge(Int age) {
    this.age = age;
}

}

like image 122
armysheng Avatar answered Sep 18 '22 15:09

armysheng


instead of

Object obj=JSONValue.parse(content.toString());
JSONObject finalResult=(JSONObject)obj;
System.out.println(finalResult);

try this:

JSONObject jsonObject = new JSONObject(content.toString());
System.out.println(jsonObject.getString("Name") + " " jsonObject.getString("Lastname") + " " + jsonObject.getInt("Age"));
like image 37
David Sousa Avatar answered Sep 17 '22 15:09

David Sousa