2

I have a JSON like

{
    "status":{
        "server":{"bool":true},
        "request":{"bool":true},
        "third_party":{"bool":true},
        "operation":{"bool":false,"int":-12,"str":"Not authenticated!"}
    },
    "response":{
        "count":3,
        "emails":["[email protected]","[email protected]","[email protected]"]
    }
}

Note: The JSON code above it's just an example and it may seem illogical.

The problem is I think it is not efficient to parse it using JSONObject and the code becomes a mess.

Therefore, I would like to know whether there is a way to parse it like in PHP when using json_decode().

Which will make me reach the elements like in this way

JSONParser jp = new JSONParser(json);
boolean server_status = jp.status.server.bool;

I wish you can understand what do I mean and help me as well.

Thank you.

5
  • 1
    What about json-simple? Commented Jul 4, 2013 at 7:43
  • And what do you want to do with that JSON? Create a pojo out of it? Navigate it? Commented Jul 4, 2013 at 7:45
  • Perhaps Java is not the correct language for this. Commented Jul 4, 2013 at 7:49
  • 1
    What do you mean by "efficiently"? Question implies readability, not performance. Is that you ask for? Commented Jul 4, 2013 at 7:49
  • Possible duplicate of stackoverflow.com/questions/338586/a-better-java-json-library even though moderator thinks it's not in proper Q&A form. Commented Jul 4, 2013 at 7:56

2 Answers 2

2

There are quite a few alternatives out there for parsing/writing JSON in java. Here are some pointers:

  1. http://jackson.codehaus.org/ This is a very complete solution which also has a StAX like API for super high processing speed.

  2. https://code.google.com/p/google-gson/ Also very complete

  3. https://code.google.com/p/json-simple/ If you need something simple that simply decodes everything to maps then you are right here.

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

Comments

0

If you want to navigate that JSON, the best library for this is, imho, Jackson. Combined with a library of mine, which has JSON Pointer support, you can do such things as:

final JsonNode node = JsonLoader.fromFile(...); // or .fromReader(), .fromString() etc
final JsonNode emails = JsonPointer.of("response", "emails")
    .get(node);
// etc

JsonNode is ultra powerful for navigating JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.