将JsonNode转换为java数组
我正在使用websocket和JsonNode开发Play framewrok 2。前端通过使用websocket连接到play framework后端。我将javascript数组转换为json节点,并通过使用webscoket连接将其发送到后端。 现在我的问题是如何将json对象转换为java数组或任何合适的结构,以便可以操作数据 这是我创建的json对象将JsonNode转换为java数组,java,json,playframework,playframework-2.0,Java,Json,Playframework,Playframework 2.0,我正在使用websocket和JsonNode开发Play framewrok 2。前端通过使用websocket连接到play framework后端。我将javascript数组转换为json节点,并通过使用webscoket连接将其发送到后端。 现在我的问题是如何将json对象转换为java数组或任何合适的结构,以便可以操作数据 这是我创建的json对象 var myjeson = {"x":arrayX,"y":arrayY} ; 这是动态填充的数组 function pixelCo
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());
}
现在我想把这些数据放在一个数组中,这样我就可以访问它们了。
任何建议都将受到欢迎。另一个建议也将受到欢迎。答案主要在和中 有几种方法可以将JsonNode转换为有用的数据 如果事先知道数据的结构,可以使用以下方法:对于“完整”绑定,创建一个对应于所有字段的类);对于结构化程度较低的方法,“原始”绑定允许您使用泛型对象 否则,这种方法应该适合你;您将在链接页面中找到的示例非常符合您的用例
请尝试这个例子,然后,如果您有更具体的问题回来与确切的实际或哲学问题 使用树模型快速完成此操作的方法。。。将JSON字符串转换为JsonNode的树:
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree("...<JSON string>...");
ObjectMapper mapper=new ObjectMapper();
JsonNode rootNode=mapper.readTree(“…”);
然后提取子节点并将其转换为列表:
List<Double> x = mapper.convertValue(rootNode.get("x"), ArrayList.class);
List<Double> y = mapper.convertValue(rootNode.get("y"), ArrayList.class);
List x=mapper.convertValue(rootNode.get(“x”),ArrayList.class);
List y=mapper.convertValue(rootNode.get(“y”),ArrayList.class);
一种稍长但有争议的更好方法是定义一个表示您期望的JSON结构的类:
public class Request {
List<Double> x;
List<Double> y;
}
公共类请求{
清单十;
列表y;
}
然后直接反序列化,如下所示:
Request request = mapper.readValue("...<JSON string>...", Request.class);
Request-Request=mapper.readValue(“…”,Request.class);
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
JsonNode rootNode=newobjectmapper().readTree(newstringreader(jsonStr));
JsonNode x=rootNode.get(“x”);
System.out.println(x.getNodeType())//应打印:数组
迭代器arrayIterator=x.Iterator();
List xList=new ArrayList();
while(arrayIterator.hasNext()){
JsonNode elementInX=arrayIterator.next();
Double xDoubleValue=elementInX.asDouble();
xList.add(xDoubleValue);
}
Double[]xArray=新的Double[xList.size()];
xList.toArray(xArray)//xArray现在应该是你想要的了
//对y做同样的事
“…如何将json对象转换为java数组?”
技巧是首先使用“elements()”方法获取迭代器对象,然后使用Guava“stream”方法获取流。一旦你有了这个流,你就可以进行各种数组操作(例如过滤、映射)来使用你的数据。将我的评论移动到一个答案,因为它得到了很多支持 这应该满足OP的需要:
newObjectMapper().convertValue(jsonNode,ArrayList.class)
感谢Stefano能够使用泛型的原始绑定实现json节点。你成就了我的一周@费萨拉杜莱:不客气:)如果我的答案有用,别忘了投票选它为好答案。如果您想分享有用的详细反馈,您仍然可以投票给我的答案,但可以添加您自己的答案。如果您正在寻找代码示例,以下是投票给通过大量链接而没有快速解决方案的人的答案newobjectmapper().convertValue(jsonNode,ArrayList.class)
@JETHALAL现在将其添加为答案。
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
import com.google.common.collect.Streams;
public void invoke(JsonNode event) {
Streams.stream(event.withArray("x").elements())
.forEach( num -> Logger.info(num.asDouble()) )
}