首页 \ 问答 \ 将JsonNode转换为java数组(Converting JsonNode to java array)

将JsonNode转换为java数组(Converting JsonNode to java array)

我正在使用websocket和JsonNode播放frame framewrok 2。 前端通过使用websocket连接到游戏框架后端。 我将javascript数组转换为json节点,并通过使用webscoket连接将其发送到后端。 现在我的问题是我如何将json对象转换为java数组或任何合适的结构,以便我可以操纵数据。

这是我创建的json对象

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)); 

在服务器端我有以下代码

 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]应用程序 -

{"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]}

现在我想将这些数据放在一个数组中,以便我可以访问它们。 任何建议都将受到赞赏。另一个建议也欢迎。


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.


原文:https://stackoverflow.com/questions/11536051
更新时间:2023-09-24 22:09

最满意答案

你可以运行查询,

  1. 循环记录并将描述一起添加到一个大的快乐字符串中。

  2. 然后,你可以通过''进入阵列爆炸

  3. 使用array_count_values()获取值数组

  4. 按降序重新排序arsort()

更新

示例代码:

$string = '';
foreach (your_result_set as one_row) 
{
    $string .= $one_row['text'];
}

$data = explode(' ', $string);
$data = array_count_values($data);
arsort($data);

You can run you query,

  1. loop through the records and append descriptions together into 1 big happy string.

  2. Then, you can explode by ' ' into array

  3. Get array of values using array_count_values()

  4. Re-sort in descending order arsort()

Update

Sample code too:

$string = '';
foreach (your_result_set as one_row) 
{
    $string .= $one_row['text'];
}

$data = explode(' ', $string);
$data = array_count_values($data);
arsort($data);

相关问答

更多

相关文章

更多

最新问答

更多
  • 使用通配符获取更多servlet请求变量[重复](Get more servlet request variables using wildcards [duplicate])
  • 返回相同的集合类型,参数化不同(Returning same collection type, differently parameterised)
  • C ++朋友函数模板重载和SFINAE在clang ++,g ++,vc ++中的不同行为(C ++ 14模式)(C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode))
  • 与paure IoT-Hub的Python paho-MQTT连接(Python paho-MQTT connection with azure IoT-Hub)
  • 编译器警告“来自不同的Objective-C类型的赋值”(Compiler warning “assignment from distinct objective-c type”)
  • C ++编译错误(在此函数中未初始化)[重复](C++ Compile Error (uninitialized in this function) [duplicate])
  • unsigned-signed下溢机制(unsigned-signed underflow mechanism)
  • 快速行查询的数据结构?(Data structure for fast line queries?)
  • 饥荒有手机安卓版的吗
  • Jquery可拖动碰撞检测错误(Jquery draggable collision detection bug)
  • sql调优是怎样来实现的?
  • 无法使占位符输入文本消失(Unable to make the placeholder input text disappear)
  • jQuery改变了两个div的CSS属性(JQuery change CSS property of two div's)
  • JDK中包含的库版本(Versions of libraries included in the JDK)
  • 请问下载的是出现ASP是什么意思
  • Firebase MLkit用于数字液晶显示器的文本识别(Firebase MLkit Text recognition for digital lcd displays)
  • 我可以在任何平台上运行C和C ++吗?(Can I run C and C++ on any platform?)
  • 让小组在C#的特定位置(get panel at specific positions in C#)
  • Nagios为通知设置了更高的间隔(Nagios set higher interval for notifications)
  • 无法向SMTP主机发送电子邮件(unable to send an email to SMTP host)
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何在.NET代码中验证全球邮政编码(How can I validate worldwide postal codes in my .NET code)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • Clojure:减少大型懒惰收集会占用内存(Clojure: Reducing large lazy collection eats up memory)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • 显示作为字符串的SVG(Showing an SVG that I have as a string)
  • 从jansson库里创建json请求的自由内存的正确方式是什么?(what is the proper way of free memory in creating json request from jansson libary?)
  • jQuery插件无法正常工作 - 它是附加的(jQuery plugin not working - it's appended)
  • 使用stat_summary自动调整ylim(Automatically adjusting ylim with stat_summary)