Creating a json object using jackson

前端 未结 4 1972
孤城傲影
孤城傲影 2020-12-05 13:26

How can I create a json array like the example below using jackson.

I tried using ObjectMapper, but this does not seem correct.

      try (Director         


        
4条回答
  •  有刺的猬
    2020-12-05 13:32

    You need a JsonNodeFactory:

    final JsonNodeFactory factory = JsonNodeFactory.instance;
    

    This class has methods to create ArrayNodes, ObjectNodes, IntNodes, DecimalNodes, TextNodes and whatnot. ArrayNodes and ObjectNodes have convenience mutation methods for adding directly most JSON primitive (non container) values without having to go through the factory (well, internally, they reference this factory, that is why).

    As to an ObjectMapper, note that it is both a serializer (ObjectWriter) and deserializer (ObjectReader).

提交回复
热议问题