15

我正在使用 websocket 和 JsonNode 开发 Play framewrok 2。前端通过 websocket 连接到 play framework 后端。我将一个 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]}

现在我想将这些数据放在一个数组中,以便我可以访问它们。任何建议将不胜感激。也欢迎提供替代建议。

4

5 回答 5

37

将我的评论移至答案,因为它得到了很多人的支持。

这应该做OP需要的:

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

于 2018-12-06T10:01:57.467 回答
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);
于 2018-03-14T22:24:39.827 回答
2

“...如何将 json 对象转换为 java 数组”

import com.google.common.collect.Streams;

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

诀窍是首先使用“elements()”方法获取 Iterator 对象,然后使用 Guava “stream”方法获取流。一旦你有了流,你就可以进行各种数组操作(例如过滤器、映射)来使用你的数据。

于 2018-11-30T14:01:06.013 回答
1

答案主要在Jackson 文档教程中;

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

如果您事先知道数据的结构,则可以使用数据绑定方法:对于“完整”绑定,请创建一个对应于所有字段的类);对于结构较少的方法,“原始”绑定允许您使用通用对象。

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

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

于 2012-07-18T11:45:45.803 回答
1

Stefano 在接受的答案中提到的树模型方法的快速 答案:

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
于 2018-11-02T11:51:17.933 回答