0

Hello everyone I have some trouble to deserialize my json with Gson. I expose my problem to you via a simple example :

This is the JSON that I receive :

"order": {
    "id": "123",
    "name": "BOBI",
    "items": {
        "totalSize": 2,
        "records": [
            {
                "id": "456",
                "Quantity": 1.0,
                "Description": "Shipping",
                "TotalAmount": 6.29,
                "TotalTaxAmount": 0.3,
                "UnitPrice": 5.99
            }
        ]
    }
}

I would like to delete/skip this useless part :

        "totalSize": 2,
            "records": [

I need a functionality like @SerializedName("items.records") to skip a level by using a Java class like this below :

public class SFOrderDto {
    
    private String id;
    
    private String name;
    
    @SerializedName("items.records")
    private List<SFProductDto> records;

}

The main call :

SFOrderDto orderArray = gson.fromJson(jsonOrder, SFOrderDto.class);

Hope somebody can help me and sorry for my bad english ! :)

Tekk
  • 1
  • 2
  • Have you tried @JsonIgnore? – papaya Oct 20 '20 at 15:50
  • You need to implement custom `JsonDeserializer` interface. See example: [Problem in parsing multiple json objects each having multiple Arrays from a validated JSON format-JavaString](https://stackoverflow.com/questions/55154226/problem-in-parsing-multiple-json-objects-each-having-multiple-arrays-from-a-vali/55170912) – Michał Ziober Oct 20 '20 at 16:07
  • @JsonIgnore will ignore all childs too.. I simply need to skip the useless level between items and records. So items will get the records array. – Tekk Oct 21 '20 at 07:56

1 Answers1

0

I found the solution here : Use jackson annotation JsonUnwrapped on a field with name different from its getter

Adding @JsonUnwrapped solved my issue.

public class SFOrderDto {
    
    private String id;
    
    private String name;
    
    @JsonUnwrapped
    @SerializedName("items.records")
    private List<SFProductDto> records;

}

Thanks to @papaya and @Michal Ziober for trying to help me :)

Tekk
  • 1
  • 2