I am completely new in Json und I am trying to deserialize a complex Json with complex types (all these Elements are Classes from my programm: entete, pied-depage, corps (List<classe like garantie,..>), nomain, labels, lieu, garantie , Elt). I try to deserialize this Json to a List from type class Tester. ``json
[{
“ entete“:{
"titre" :, "Nummer" : ,
… }
“ pied-depage“:{
"utilisateur" : "martin"
… }
“ Corps“:[{
"reservation" : 0.00,
"garantie" : [ ],
"Utilisateurs" : [ ],
…}
…}]
“ nomain“: $
“ daterRange“: {}
“ labels“:{}
"date1" : "1988-01-01",
"date2" : "1528-12-31"
}]
```
```java Code
Class Tester{
private entete header = entete.getHeader();
private pied-depage footer = pied-depage.getFooter();
private List<Elt> Corps;
private Elt anlagengut;
private String nomain;
private LocalDateRange dateRange;
public LocalDate getDate1() {
return this.dateRange.getStart();
}
public LocalDate getDate2() {
return this.dateRange.getEndInclusive();
}
getter ...
}
public static List<Tester> getCollection() throws IOException
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModules( new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
.findAndRegisterModules();
List<Tester> tester = (List<Tester>) mapper.readValue(Paths.get("Tester.json").toFile(), List.class);
return tester;
}
```
I am trying after serialization of the same Object with Jackson to deserialize the produced Json to have the identic container in another program. do you have any indication for me? I have already written these but I get an object with elements of Type LinkedHashMap
[{
“ entete“:{
"titre" :, "Nummer" : ,
… }
“ pied-depage“:{
"utilisateur" : "martin"
… }
“ Corps“:[{
"reservation" : 0.00,
"garantie" : [ ],
"Utilisateurs" : [ ],
…}
…}]
“ nomain“: $
“ daterRange“: {}
“ labels“:{}
"date1" : "1988-01-01",
"date2" : "1528-12-31"
}]
```
Kindly see reference below:
When you see { }
, You can either create a Class to map the properties, or you can use Map<String, String>
When you see [{}]
, You can use a List<YourCustomClass>
or List<Map<String, String>>
Use @JsonProperty("YourWeirdPropertyNameHere")
if you want to map your Class Properties towards JSON Properties which have different cases/names
When mapping properties,
entete“:{
"titre" :, "Nummer" : ,
… }
Then a class representation will look like:
public class Entete {
private String titre; //String because the value is "Number" which is in ""
}
For
“ Corps“:[{
"reservation" : 0.00,
"garantie" : [ ],
"Utilisateurs" : [ ],
…}
…}]
Then a class representation will look like:
public class Corp{
private Double reservation;
private List<String> garantie;
@JsonProperty("Utilisateurs")
private List<String> utilisateurs;
}
Then you can create the mapper class Test:
public class Test{
public Entete entete;
@JsonProperty("pied-depage")
public PiedDepage piedDepage;
@JsonProperty("Corps")
public List<Corp> corps;
}
And to use ObjectMapper, you need to pass TypeReference:
List<Test> myTests = mapper.readValue(jsonInput, new TypeReference<List<Test>>(){});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.