0

So I've created a basic REST API in PHP that returns the following:

$result = array(
    "var" => 'testvalue',
);

sendResponse(200, json_encode($result));

How would I get this testvalue from the JSON using a HTTP POST request in Java?

I'm using the recommended answer from here currently: Sending HTTP POST Request In Java

But it dosn't show how to convert it from this stage:

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();

To a Java object.

So pretty much how to convert from JSON to a Java object.

Any ideas?

Community
  • 1
  • 1

1 Answers1

1

If you do not need to read large data, you can use something like:

JSONObject json = new JSONObject(EntityUtils.toString(response.getEntity()));
String var = json.getString("var"):

You can use it on Android too, the library is org.json.JSONObject.

Bob
  • 436
  • 5
  • 13
  • It could be nice to mention from which library/package JSONObject comes from because it is not standard Java class. – Pshemo Oct 01 '14 at 17:57
  • @Pshemo org.json.JSONObject – Bob Oct 01 '14 at 18:14
  • It would be nice if you put that information with appropriate link to project site or jars (and/or maven dependency) to your answer. – Pshemo Oct 01 '14 at 18:15