0

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

    Map<String, Map<String, Integer>> map =
new GsonBuilder().create().fromJson(json, new HashMap<String, HashMap<String, Integer>>().getClass());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.