将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
最满意答案
你可以运行查询,
循环记录并将描述一起添加到一个大的快乐字符串中。
然后,你可以通过''进入阵列爆炸
使用array_count_values()获取值数组
按降序重新排序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,
loop through the records and append descriptions together into 1 big happy string.
Then, you can explode by ' ' into array
Get array of values using array_count_values()
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);
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
一种方法可以是将apple, oranges, pears转换为apple|oranges|pears并使用RLIKE (即正则表达式)来匹配它。 例如,'John喜欢吃苹果'匹配正则表达式'apple | orange | pears'。 首先,将'apple,oranges,pears'转换为正则表达式形式,将所有','替换为'|' 使用REPLACE 。 然后使用RLIKE选择匹配的关键字条目: SELECT * FROM keywords_table WHERE 'John liked to ea ...
-
使用'like'赋值器可以将值字段与部分字符串匹配。 在这种情况下, %充当外卡,因此%tech%匹配包含'tech'的所有查询: mysql_query("select * from news where keyword like '%tech%' limit lowervalue, highervalue"); 例 mysql_query("select * from news where keyword like '%tech%' limit 5"); Using the 'like' evaluat ...
-
有了LIKE你有三种方法。 1)如果它包含相同顺序的所有单词(关键词:一架飞机) SELECT * FROM descriptions WHERE description LIKE('%One airplane%'); 2)如果它包含所有单词但其他顺序无关紧要 SELECT * FROM descriptions WHERE description LIKE('%One%') AND description LIKE('% airplane%'); 3)如果它包含一些单词 SELECT * FROM d ...
-
标准化方式: 创建一个像这样的表格并继续进行简单的查询,这是一般的想法而不是详细的解决方案,但它的工作原理: exampleId | file | type -------------|------------|---------- 1 | file1 | - 1 | file2 | and 1 | file3 | and 2 | file1 ...
-
在php端,有很多选项来计算数组中的关键字。 如果您需要其他功能,如无壳匹配或字边界 ,请使用正则表达式 。 使用preg_match_all的想法 返回完整模式匹配的数量(可能为零),如果发生错误则返回FALSE。 $pattern = '~(?:yes|test)~i'; foreach($arr AS $k => $v) $arr[$k]['match'] = preg_match_all($pattern, $v['title']." ".$v['msg']); 该模式只是使用非捕获组的两个 ...
-
你可以运行查询, 循环记录并将描述一起添加到一个大的快乐字符串中。 然后,你可以通过''进入阵列爆炸 使用array_count_values()获取值数组 按降序重新排序arsort() 更新 示例代码: $string = ''; foreach (your_result_set as one_row) { $string .= $one_row['text']; } $data = explode(' ', $string); $data = array_count_values($dat ...
-
尝试这个: SELECT title FROM mytable WHERE title LIKE '%Ho%windo%' Try this: SELECT title FROM mytable WHERE title LIKE '%Ho%windo%'
-
试试这个查询 SELECT * FROM ( SELECT *, @rowNo := if(@pv = week, @rowNo+1, 1) as rNo, @pv := week FROM ( SELECT keyword_id, COUNT(*), YEARWEEK(FROM_UNIXTIME(created)) AS week FROM keyword_sentence WHERE FROM_UNIXTIME(c ...
-
我不认为这里需要加入。 只需列出所有关键字以及关键字出现的次数,从最常见到最低频率排序。 SELECT keyword, COUNT(*) freq FROM keywordTable GROUP BY keyword ORDER BY freq DESC I don't see the need for a join here. Simply list all the keywords along with the number of times the keyword appears, orde ...