5

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?

jojemapa
  • 873
  • 2
  • 10
  • 21

3 Answers3

4

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;
}

}

armysheng
  • 133
  • 7
3

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"));
David Sousa
  • 120
  • 2
  • 8
  • JSONObject does not accept the string argument. The method getString not exist. Thanks for your time ;( – jojemapa Feb 21 '16 at 01:33
  • I think you are using a native library, because i used an external json jar and worked fine, I hope you can solve your problem ^^ – David Sousa Feb 21 '16 at 01:45
  • Thank you. Apparently you use json javax . It is not the same as Gson , or single - json . And can resolve my problem. Thank you. – jojemapa Feb 21 '16 at 03:56
  • 1
    @VipPunkJoshersDroopy [org.json.JSONObject](http://developer.android.com/reference/org/json/JSONObject.html#getString%28java.lang.String%29) does have a `getString(String)` method. Though, always make sure to check with `has(String)` beforehand if the JSON actually contained this field on the respective object – Roman Vottner Feb 21 '16 at 04:28
0

I higly recomend http-request built on apache http api.

HttpRequest<Data> httpRequest = HttpRequestBuilder.createGet(yourUri, Data.class)
    .addDefaultHeader("accept", "application/json")
    .build();

public void send(){
   ResponseHandler<Data> responseHandler = httpRequest.execute();
   Data data = responseHandler.orElseThrow(); // returns the data or throws ResponseException If response code is not success
}

Data class which you get as response.

public Data{
   private String Name;
   private String Lastname;
   private int Age;

   // getters and setters
}

I also recommend watching my answer here If you want get response as String

Beno
  • 945
  • 11
  • 22