转换JsonNode到Java阵列(Converting JsonNode to java arra

2019-07-30 11:45发布

我正在玩framewrok 2的WebSocket和JsonNode。 前端连接到由使用的WebSocket的后端播放框架。 我转换JavaScript阵列成JSON节点和通过使用所述webscoket连接的它发送到后端。 现在,我的问题是如何转换的JSON对象为AA 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());
    }

当我检查的消息传递日志:下面是日志信息[信息]应用 -

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

现在我想将这些数据在阵列中,这样我可以访问它们。 任何建议将appreciated.an其他建议也欢迎。

Answer 1:

答案多半是在杰克逊的文档和教程 ;

你有几个方面与JsonNode转换成有用的数据。

如果事先知道数据的结构,你可以使用数据绑定的方法:为“满”的结合做出相应的所有字段)一类; 对于一个不太结构化的方法“原始”的结合让你使用通用的对象。

否则, 树模型的方法应该为你工作; 您将在链接页面找到实例对应得很好,以你的使用情况。

请尝试例子,那么,如果你有更具体的问题,带回来的确切实际或phylosophical问题!



Answer 2:

移动我的评论一个答案,因为它得到了upvoted了很多。

这应该需要做的是什么OP:

new ObjectMapper().convertValue(jsonNode, ArrayList.class)



Answer 3:

一个快速的方法来做到这一点使用树模型... JSON字符串转换成JsonNode的树:

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree("...<JSON string>...");

然后将解压后的子节点,并将其转换成列表:

List<Double> x = mapper.convertValue(rootNode.get("x"), ArrayList.class);
List<Double> y = mapper.convertValue(rootNode.get("y"), ArrayList.class);

轻微的时间较长,但值得商榷更好的方法来做到这一点是确定代表你所期望的JSON结构类:

public class Request {
    List<Double> x;
    List<Double> y;
}

然后直接反序列如下:

Request request = mapper.readValue("...<JSON string>...", Request.class);


Answer 4:

"... how do I convert the json object into a java array"

import com.google.common.collect.Streams;

public void invoke(JsonNode event) {                 
    Streams.stream(event.withArray("x").elements())
        .forEach( num -> Logger.info(num.asDouble()) )
}

The trick is to first get the Iterator object using "elements()" method, then using Guava "stream" method get the stream. Once you have the stream you can do all kinds of array operations(e.g. filter, map) to consume your data.



Answer 5:

由斯特凡诺在接受的答案中提到的树模型的方法快速的答案:

JsonNode rootNode = new ObjectMapper().readTree(new StringReader(jsonStr));
JsonNode x=rootNode.get("x");
System.out.println(x.getNodeType());//should print: ARRAY
Iterator<JsonNode> arrayIterator=x.iterator();
List<Double> xList=new ArrayList<Double>();
while(arrayIterator.hasNext()) {
    JsonNode elementInX=arrayIterator.next();
    Double xDoubleValue=elementInX.asDouble();
    xList.add(xDoubleValue);
}
Double[] xArray=new Double[xList.size()];
xList.toArray(xArray);//xArray now should be what you want

//do the same thing to y


文章来源: Converting JsonNode to java array