0

Java : How to convert :

From Object String(java.lang.Object) :

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

To 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());

I get

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

But I need

actionInfo = {"rule_id":7528,"name":"JAY_TEST_RULE"}
Jay Teli
  • 530
  • 1
  • 11
  • 19
  • 1
    Possible duplicate of [Convert Java Object to JsonNode in Jackson](http://stackoverflow.com/questions/11828368/convert-java-object-to-jsonnode-in-jackson) – DimaSan Dec 14 '16 at 12:38
  • 2
    Possible duplicate of [Jackson: is there a way to serialize POJOs directly to treemodel?](http://stackoverflow.com/questions/6967583/jackson-is-there-a-way-to-serialize-pojos-directly-to-treemodel) – choasia Dec 14 '16 at 12:39
  • Your object is an array, you can convert it to JsonArray – thepaulo Dec 14 '16 at 12:42
  • I had checked ur links before.. it gives me escaped jsonArray instead on JsonNode.. please check updated question again. thank you – Jay Teli Dec 14 '16 at 12:52

1 Answers1

0

I solved it:

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

Above will give me Array of JsonString , to convert it to JsonString i used substring method as below.

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

Jay Teli
  • 530
  • 1
  • 11
  • 19