即使JSON文件具有键的值,也会将值作为null检索

xn1cxnb4  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(102)

我有一个JSON文件,看起来像这样:

{
  "caseNumber": "kblndw896",
  "features": [
    {
         "id1": "wad12jkb",
         "id2": "jBgioj",
         "id3": "jabd8&",
         "limit": "500000",
         "M1": "333487.2",
         "M2": "339259.88",
         "M3": "316003.84",
         "M4": "219097.12",
         "M5": "393842.98",
         "M6": "498684.1",
         "M7": "1074002.44",
         "M8": "437211.19",
         "M9": "444842.35",
         "M10": "657756.22",
         "M11": "571928.78",
         "M12": "230378.06"
      }
  ]
}

现在我尝试导入这个文件,并放入名为Payload的POJO类,它具有POJO类的子特性。现在,当我使用ObjectMapper导入时:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Payload payload= mapper.readValue(new File("filepath\\file.json"),
                Payload.class);

然后我把它导入到payload POJO类中,但是features数组中的键的值对于所有键都是null值。
例如,导入的数据为:

{
  "caseNumber": "kblndw896",
  "features": [
    {
      "id1": null,
      "id2": "null",
      "id3": "null",
      "limit": null,
      "m7": null,
      "m6": null,
      "m9": null,
      "m4": null,
      "m11": null,
      "m8": null,
      "m2": null,
      "m12": null,
      "m1": null,
      "m3": null,
      "m5": null,
      "m10": null
    }
  ]
}

如何从文件中获取值?
尝试:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Payload payload= mapper.readValue(new File("filepath\\file.json"),
                Payload.class);

预期:

{
  "caseNumber": "kblndw896",
  "features": [
    {
         "id1": "wad12jkb",
         "id2": "jBgioj",
         "id3": "jabd8&",
         "limit": "500000",
         "M1": "333487.2",
         "M2": "339259.88",
         "M3": "316003.84",
         "M4": "219097.12",
         "M5": "393842.98",
         "M6": "498684.1",
         "M7": "1074002.44",
         "M8": "437211.19",
         "M9": "444842.35",
         "M10": "657756.22",
         "M11": "571928.78",
         "M12": "230378.06"
      }
  ]
}
wlzqhblo

wlzqhblo1#

我会这样做:
这是class Features

public class Features {
    private String id1;
    private String id2;
    private String id3;
    private String limit;
    private String m1;
    private String m2;
    private String m3;
    private String m4;
    private String m5;
    private String m6;
    private String m7;
    private String m8;
    private String m9;
    private String m10;
    private String m11;
    private String m12;

    // getters and setters below
}

这是class Payload

public class Payload {
    private String caseNumber;
    private List<Features> features;

    // getters and setters below
}

最后:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Payload payload = objectMapper.readValue(new File("filepath\\file.json"), Payload.class);
System.out.println(payload);

相关问题