Reputation: 971
I have a sample json string in the format below
{
"sample":{
"1":{
"a":100,
"b":101
},
"2":{
"a":200,
"b":101
},
"3":{
"a":100,
"b":102
}
}
}
Is there a way to retreive the data in the format Map(String,Map (String,Integer)). In the inner map I need only the first element "a". I understand we can define a new class structure and get the data and get the same transformed through ObjectMapper, which is not requirement in my case.Appreciate the help.
Upvotes: 0
Views: 223
Reputation: 2585
the only way I see, is that you transform your Object in the needed representation before sending it through the mapper.
In any case you should, from my point of view, try to avoid to manipulate the generated JSON since it can easily lead to confusion in production or if another developer has to take over your job.
It's a much cleaner way to define a DTO which expresses the needed representation.
Best Regards
Upvotes: 2
Reputation: 4702
You can try this:
Map<String, Map<String, Integer>> map =
new GsonBuilder().create().fromJson(json, new HashMap<String, HashMap<String, Integer>>().getClass());
Upvotes: 1