1

From the endpoint "test" I am returning a JSONObject:

@POST("/test")
    @PermitAll
    public JSONObject test(String name) {
    JSONObject jsonval=new JSONObject();
    json.put("key1",true);
    json.put("key2","test");
        return json;
    }

in the method that checks the returned value I want to search for value of "key1".

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String json = null;
    String res = "";

    while ((res = in.readLine()) != null) {
        json += res + "\n";
        }
    in.close();

    if (jsonData has key1 with value true){
    //do sth
    }
    else{
    //do sth else
    }

How can I parse the returned JSONObject?

2
  • What's the jar that you are using for obtaining the class JSONObject? Commented Mar 27, 2015 at 18:04
  • json-simple-1.1.1.jar Commented Mar 27, 2015 at 18:06

1 Answer 1

1

Have you tried constructing the JSONObject from its string representation (see http://www.json.org/javadoc/org/json/JSONObject.html):

JSONObject result = new JSONObject(json)

where json is the string you've read from the InputStream

Note: you might have to strip the last new line char or even omit new lines altogether

Sign up to request clarification or add additional context in comments.

Comments

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.