2

I have the following as a json output, how do I convert it to a HashMap using Gson:

{
    "result": {
        "haystack": [
            "SvqFd",
            "ucR3Y",
            "AzVB0",
            "DrDQL",
            "gc8LQ",
            "JqKdK",
            "9ZE4g",
            "820gw",
            "ekWkr",
            "qgDrR",
            "ENpyZ",
            "lf4b8",
            "SZsUt",
            "YO0cQ",
            "CD1O0",
            "Rvw8t",
            "euSC0",
            "3oemT",
            "6bUgC",
            "m8pFK"
        ],
        "needle": "lf4b8"
    }
}

I tried:

Type stringArrayMap = new TypeToken<Map<String, String[]>>(){}.getType(); 
Map<String,String[]> theCollection = gson.fromJson(output, stringArrayMap);

Edit: Each time I get this json output,its content is unique but same structure.

ANSWER

Answers and suggestions below worked but I played around with those and figured something else that worked as well;

Type stringStringObjectMap = new TypeToken<Map<String, Map<String, Object>>>(){}.getType(); 
Map<String, Map<String, Object>> theDictionary = gson.fromJson(output, stringStringObjectMap); 
1

1 Answer 1

2

1.) Parse Json String to create JSONObject.

2.) Iterate over JsonObject to create Map.

you might get here what you want

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

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.