Best way to use Jackson JsonNodeFactory

前端 未结 4 413
北恋
北恋 2021-01-30 22:34

I\'m using Jackson to build a custom JSON object. Is the correct way of going about this?

It seems to work well (and the output is correct) but I may be missing the way

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 23:02

    Just a suggestion, it would be easier to directly deal with simple datatypes and serialize those to JSON and back using Jackson ObjectMapper, rather than deal with raw Jackson Treemodel

    So in your example, you could create a structure of the following type:

    class AaData{
        private List rowList = new ArrayList();
    ..
    
    class ARow{
        String ounces;
        String revolutions;
    ..
    

    Then the following will generate the appropriate json for you:

    StringWriter sw = new StringWriter();
    JsonFactory jf = new JsonFactory();
    ObjectMapper m = new ObjectMapper();
    m.writeValue(sw, aaData);
    System.out.println(sw.toString());
    

提交回复
热议问题