Java:将Java对象转换为JacksonJsonNode

时间:2016-12-14 12:37:45

标签: java json jackson

Java:如何转换:

从 Object String(java.lang.Object):

String [{"rule_id":7528,"name":"JAY_TEST_RULE"}]

要 JsonNode(com.fasterxml.jackson.databind.JsonNode):

{"rule_id":7528,"name":"JAY_TEST_RULE"} 


My Attempts :

Using 

Object actionInfoValue = memcachedObject.getDataForKey(actionInfoKey); 

ObjectMapper mapper = new ObjectMapper();

actionInfo = mapper.convertValue(actionInfoValue,JsonNode.class);

OR 

actionInfo = mapper.valueToTree(actionInfoValue.toString());

我得到了

actionInfo = "[{\"rule_id\":7528,\"name\":\"JAY_TEST_RULE\"}]"

但我需要

actionInfo = {"rule_id":7528,"name":"JAY_TEST_RULE"}

1 个答案:

答案 0 :(得分:0)

我解决了它:

  

String actionInfoValue =(String)memcachedObject.getDataForKey(actionInfoKey);

上面会给我一个JsonString数组,将它转换为JsonString我使用了substring方法,如下所示。

  

actionInfoValue = actionInfoValue.substring(1,actionInfoValue.length() - 1);