How to convert JsonNode to ObjectNode

前端 未结 4 1754
误落风尘
误落风尘 2021-02-05 03:51

I have a com.fasterxml JsonNode object with some data. I need to do some manipulation on its data. I googled for answer but didn\'t got it properly. Can you please

相关标签:
4条回答
  • 2021-02-05 04:33

    I had this error too although in my case it was a stupid mistake. I accidentally imported org.codehaus.jackson.node.ObjectNode instead of com.fasterxml.jackson.databind.node.ObjectNode. Using the jackson ObjectNode fixed the issuse.

    0 讨论(0)
  • 2021-02-05 04:38

    I try it some times, it will be ok! You only define a Student Class to map the properties. Then you could convert the jsonNode to Student Object.

    Student student = objectMapper.convertValue(jsonNode1, Student.class);

    I think this will be suitable for your need!

    0 讨论(0)
  • 2021-02-05 04:41

    You can convert a JsonNode in an ObjectNode in this simple way:

    ObjectNode objectNode = jsonNode.deepCopy();
    

    Available from Jackson 2.0 and tested with Jackson 2.4.0

    0 讨论(0)
  • 2021-02-05 04:43

    Finally, I got the solution as follows...

    JsonNode jsonNode = Json.toJson("Json String");
    ObjectNode node = (ObjectNode) new ObjectMapper().readTree(jsonNode.asText());
    //perform operations on node
    jsonNode = (JsonNode) new ObjectMapper().readTree(node.toString());
    

    or another one as below...

    ObjectNode node = (ObjectNode) new ObjectMapper().readTree("Json String")
    //perform operations on node
    jsonNode = (JsonNode) new ObjectMapper().readTree(node.toString());
    

    but I don't know if this is good approach or not ? If there is any better than above, please let me know. Thank you!

    0 讨论(0)
提交回复
热议问题