I have a JSON file which looks like:
{
"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"
}
]
}
Now I’m trying to import this file and put in the POJO class called Payload, which has features POJO class as child. Now when I import using ObjectMapper
:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Payload payload= mapper.readValue(new File("filepath\file.json"),
Payload.class);
then I’m getting this imported in the payload POJO class, but the values for keys in features array has null value for all the keys.
Example, the imported data is:
{
"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
}
]
}
How can I get values from file?
Tried:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Payload payload= mapper.readValue(new File("filepath\file.json"),
Payload.class);
Expected:
{
"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"
}
]
}
2
Answers
I would do it this way:
This is
class Features
This is
class Payload
And finally:
You can try the provided method below. It makes use of the org.json.simple-1.1.1.jar library which you can download from here:
The getKeyValuesFromJsonArray() Method:
How you might use the above method:
The Feature Class: