Map from Java to Mongo an object with custom structure

2

I'm using a Mongo database and the backend with Java Spring. Originally it had this structure:

"defaultActivation":{
    "accounts": ["500026", "500027"]
}

And I mapped it with the following structure in Java:

private Map<String, Set<String>> defaultActivation;

But now the structure is a bit more complex:

"defaultActivation":{
    "accounts": ["500026", "500027"],
    "paymentsModel": {
        "TSP": ["CO", "AR"],
        "PSP": ["CO"]
    }
}"

How can I map this new structure, should I create another model in Java?

    
asked by AndreFontaine 30.06.2016 в 22:24
source

1 answer

2

You can use Gson for this kind of thing, specifically your problem can be posed in the following way:

You have this Json

"defaultActivation":{
    "accounts": ["500026", "500027"],
    "paymentsModel": {
        "TSP": ["CO", "AR"],
        "PSP": ["CO"]
    }
}"

You should create an object (class) PaymentModel with the attributes TSP and PSP that are of type array. For the parent object you should create a class DefaultActivation with the attributes account of type array and the other attribute would be type PaymentModel

With an instance of Gson you should only make a call to

gson.fromJson(json, DefaultActivation.class);
    
answered by 01.07.2016 / 21:34
source