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"}
答案 0 :(得分:0)
我解决了它:
String actionInfoValue =(String)memcachedObject.getDataForKey(actionInfoKey);
上面会给我一个JsonString数组,将它转换为JsonString我使用了substring方法,如下所示。
actionInfoValue = actionInfoValue.substring(1,actionInfoValue.length() - 1);