Get jsonvalue without a name

i’m struggling with a JSONObject. I allready returned some json and converted it succesfully to objects and list of objects. My now i’m stuck.

This is the JSONObject i’m getting:

{"Result":true,"Messages":["Goe bezig!"]}

I’m able to get the Messages, but i can’t seem to get the boolean in Result. Can somebody explain how to get it plz?

Here is the code:

public boolean Convert(JSONObject json) {
    try 
    {
        return json.getBoolean("Result");
    } 
    catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        return false;
    }
}

Answer

This worked fine for me, although it’s pretty vague your question.

    String jsonString = "{"Result":true,"Messages":["Goe bezig!"]}";

    JSONObject jsonObject = new JSONObject(jsonString);

    boolean result = (Boolean) jsonObject.get("Result");

    System.out.println(result);

You might want to catch at the end of your method Exception as well:

try {
   return json.getBoolean("Result");
} catch (JSONException e) {
   e.printStackTrace(); // replace these with `Log` statement

   return false;
} catch (Exception e) {
   e.printStackTrace(); // replace these with `Log` statement

   return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *