Reputation:
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.
Upvotes: 2
Views: 125
Reputation: 11712
There are quite a few alternatives out there for parsing/writing JSON in java. Here are some pointers:
http://jackson.codehaus.org/ This is a very complete solution which also has a StAX like API for super high processing speed.
https://code.google.com/p/google-gson/ Also very complete
https://code.google.com/p/json-simple/ If you need something simple that simply decodes everything to maps then you are right here.
Upvotes: 2
Reputation: 121720
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.
Upvotes: 0