我正在使用 Java Play 框架中的 AKKA Actors 创建一个 Web 套接字处理程序。我正在尝试解析消息并创建 JsonNode。我试过了
public void onReceive(Object message) throws Exception
{
if (message instanceof String)
{
JsonNode clientMessage =Json.toJson(message.toString());
System.out.println(clientMessage);//"{\"messageTxt\":\"hello\",\"id\":\"123\"}"
ObjectMapper objectMapper = new ObjectMapper();
try
{
JsonNode node = objectMapper.readValue(clientMessage, JsonNode.class);
JsonNode messageNode = node.get("messageTxt");
String messagec = messageNode.asText();
System.out.println("message = " + messagec);
JsonNode idNode = node.get("id");
int id = idNode.asInt();
System.out.println("id = " + id);
}
catch (Exception e)
{
e.printStackTrace();
}
/*
out.tell( response, self());
*/
}
}
我收到一个错误
[error] no suitable method found for readValue(com.fasterxml.jackson.databind.JsonNode,java.lang.Class<com.fasterxml.jackson.databind.JsonNode>)
[error] (argument mismatch; com.fasterxml.jackson.databind.JsonNode cannot be converted to com.fasterxml.jackson.core.JsonParser))
如何将对象转换JsonNode
为JsonParser
对象?或者有没有更好的方法来创建 JSON/Map 对象。