将JsonNode转换为Java数组 [英] Converting JsonNode to java array

查看:955
本文介绍了将JsonNode转换为Java数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用websocket和JsonNode开发Play framewrok 2.前端通过使用websocket连接到play框架后端.我通过使用webscoket连接将一个javascript数组转换为json节点并将其发送到后端. 现在我的问题是如何将json对象转换为Java数组或任何合适的结构,以便可以处理数据.

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.

这是我创建的json对象

this is the json object I created

var myjeson = {"x":arrayX,"y":arrayY} ;

这是动态填充的数组

 function pixelCount ()
    {  arrayX[counter] = xcoordinate;        
    arrayY[counter] = ycoordinate;
    socket.send(" from array X,Y  "+arrayX[counter]+ " " +arrayY[counter]); 
    ++counter;      
    }

下面的代码发送数据

$('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());
    }

当我检查日志时,消息已传递: 以下是日志信息 [info]应用程序-

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.

推荐答案

答案大多在 Jackson文档中教程;

您有几种方法可以将JsonNode转换为有用的数据.

you have several ways to transform that JsonNode into useful data.

如果您事先知道数据的结构,则可以使用

If you know in advance the structure of the data you can use the Data Binding approach: for "full" binding make a Class corresponding to all fields); For a less structured approach the 'raw' binding let you use generic objects.

否则,树模型方法应该为您工作;在链接页面中找到的示例与您的用例非常吻合.

Otherwise the Tree Model approach should work for you; the example you will find in the linked page corresponds quite well to your use case.

请尝试该示例,然后,如果您还有更多具体问题,请返回确切的实际问题或哲学问题!

Please try that example, then if you have more specific issues come back with the exact practical or phylosophical issues!

这篇关于将JsonNode转换为Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆