I am working on Play framewrok 2 with websocket and JsonNode . the front end is connected to the play framework backend by use of websocket. I converted a javascript array into a json node and sent it to the backend by the use of the webscoket connection. Now my problem is how do i convert the json object into a a java array or any suitable structure so that i can manipulate the data.
this is the json object I created
var myjeson = {"x":arrayX,"y":arrayY} ;
this is the array which is populated dynamically
function pixelCount ()
{ arrayX[counter] = xcoordinate;
arrayY[counter] = ycoordinate;
socket.send(" from array X,Y "+arrayX[counter]+ " " +arrayY[counter]);
++counter;
}
the code below sends the data
$('button.send').click(function() {
sock.send(JSON.stringify(myjeson));
on the server side i have the following code
public static WebSocket<JsonNode> givenIn() {
return new WebSocket<JsonNode>() {
// called when the websocket is established
public void onReady(WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out) {
// register a callback for processing instream events
in.onMessage(new Callback<JsonNode>() {
public void invoke(JsonNode event) {
Logger.info(event.toString());
}
when I check the log the message is delivered : below is the log info [info] application -
{"x":
[78.72727298736572,79.72727298736572,82.72727298736572,
7298736572,93.72727298736572,83.72727298736572132.72727298736572],
"y":
[82.6363639831543,82.6363639831543,63.54545593261719,63.54545593261719,64.545455932
61719,65.54545593261719,70.54545593261719,189.5454559326172,188.5454559326172]}
Now I will like to place these data in an array so that I can access them. any suggestion will be appreciated.an alternative suggestion is also welcomed.
Traverse JsonNode Graph You do so by iterating its nested fields (or nested elements in case of an array). Here is an example of traversing all nested fields of a JsonNode representing a JSON object or JSON array: public static void traverse(JsonNode root){ if(root. isObject()){ Iterator<String> fieldNames = root.
Core Java bootcamp program with Hands on practice A JsonNode is a base class for all JSON nodes that forms the JSON Tree Model whereas ArrayNode is a node class that represents an array mapped from JSON content.
A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class.
A ObjectNode provides a node for a linked list with Object data in each node.
Moving my comment to an answer, as it got upvoted a lot.
This should do what the OP needed:
new ObjectMapper().convertValue(jsonNode, ArrayList.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With