如何使用带有列表的杰克逊树模型反序列化嵌套的json文件

时间:2019-07-08 19:48:09

标签: java json jackson deserialization

我有一个嵌套的json文件,想将其转换为POJO。 json看起来像这样: -我想要一个路径列表(或数组) -每个路径都有技能,如何将每个路径元素链接到其技能列表(或数组) -如何处理布尔值和日期时间格式?

我尝试了杰克逊树模型,可以使用打印整个文件,但是如何将它们转换为Java对象和对象数组, 另外,有些文件只有一个path元素,有些只有多个path元素,所以我相信动态列表会更合适?

json文件:

{"paths":[
         {"name" : "abc",
          "age" :23,

          "Skills": [
                      {"date": "2019-07-11 19:09:20", "level" :123, 

            "garbage" :{"filed_a":"aa", "field_b" : "bb"},

              "efficiency" : 79},

                      {"date": "2019-08-09 18:08:53", "level" :136,

             "garbage" :{"filed_a":"aa2", "field_b" : "bb3"},

               "efficiency" : 80},

                      {"date": "2019-03-17 12:06:43", "level" :163,

                "garbage" :{"filed_a":"aa3", "field_b" : "bb6"},

                   "efficiency" : 80}

                     ],

            "status" : "junior",
            "field" : "agriculture",
            isTrue : false
            },

        {"name" : "cbd",
          "age" :43,

          "Skills": [
                      {"date": "2018-08-18 18:08:28", "level" :432, 

            "garbage" :{"filed_a":"aa5", "field_b" : "bb7"},

              "efficiency" : 20},

                      {"date": "2019-08-09 18:08:53", "level" :136,

             "garbage" :{"filed_a":"aa55", "field_b" : "bb77"},

               "efficiency" : 30},

                      {"date": "2019-03-17 12:06:43", "level" :163,

                "garbage" :{"filed_a":"aa555", "field_b" : "bb777"},

                   "efficiency" : 60}

                     ],

            "status" : "junior",
            "field" : "agriculture",
            "isTrue" : true
            }
        ....
           ]
}

这就是我到目前为止所拥有的,它只是将内容打印到控制台:

ObjectMapper mapper = new ObjectMapper();

        // Read JSON file and convert to java object
        FileInputStream fileInputStream = null;
        try
        {
            String filename = "myfile.json";
            fileInputStream = new FileInputStream(filename);
            JsonFactory factory = mapper.getFactory();
            JsonParser parser = factory.createParser(fileInputStream);
            JsonNode actualObj = mapper.readTree(parser);
            JsonNode jsonNode1 = actualObj.get("paths");
            System.out.println(jsonNode1.toString());
            fileInputStream.close();

        } catch (IOException e)
        {
            e.printStackTrace();
        }

0 个答案:

没有答案