Reputation: 480
I haved parse a JSON wich PHP in few minutes, it's very easy.
I need to do the same things in JAVA and it's more complicated. I have choose Jackson.
Here the JSON:
{
"object":"page",
"entry":[
{
"id":"1849584656581184912",
"time":1361458605,
"changes":[
{
"field":"feed",
"value":{
"item":"post",
"verb":"add",
"post_id":"6022322264458251"
}
}
]
},
{
"id":"184965658184912",
"time":1361458606,
"changes":[
{
"field":"feed",
"value":{
"item":"comment",
"verb":"add",
"comment_id":"1849584656581184912_6022322264458251_7510038",
"parent_id":"1849584656581184912_6022322264458251",
"sender_id":657754651107,
"created_time":1361458606
}
},
{
"field":"feed",
"value":{
"item":"comment",
"verb":"add",
"comment_id":"1849584656581184912_6022322264458251_7510037",
"parent_id":"1849584656581184912_6022322264458251",
"sender_id":657754651107,
"created_time":1361458606
}
}
]
}
]
}
Here the PHP code:
foreach($object["entry"] as $update)
{
// For each entry in notification, display the entry
echo "page id = " . $update["id"];
echo "time = " . $update["time"];
foreach($update["changes"] as $change) {
echo "field = " . $change["field"];
echo "verb = " . $change["value"]["verb"];
if($change["value"]["item"] == "comment") {
echo "Nouveau commentaire publié !";
echo "comment_id = " . $change["value"]["comment_id"];
echo "parent_id = " . $change["value"]["parent_id"];
echo "sender_id = " . $change["value"]["sender_id"];
echo "created_time = " . $change["value"]["created_time"];
} else if($change["value"]["item"] == "post") {
echo "Nouveau post publié !";
echo "post_id = " . $change["value"]["post_id"];
}
}
}
So I need the same things, to parse this JSON and display some informations.
Do you know a simple way to do this, like in PHP?
Thanks
Upvotes: 0
Views: 313
Reputation: 4975
If you only want to access some fields directly then you should use JsonNodes.
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonString);
JsonNode entry = root.get("entry");
…
Edit: Be sure to have a look on all concrete subclasses. For example an ArrayNode lets you iterate over all of its elements.
Same code as above with casts:
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = (ObjectNode) Jsmapper.readTree(jsonString);
ArrayNode entry = (ArrayNode) root.get("entry");
…
Upvotes: 2