How can I convert JSON to a HashMap using Gson?

前端 未结 16 2683
臣服心动
臣服心动 2020-11-22 05:14

I\'m requesting data from a server which returns data in the JSON format. Casting a HashMap into JSON when making the request wasn\'t hard at all but the other way seems to

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 05:56

    Update for new Gson lib:
    You now can parse nested Json to Map directly, but you should be aware in case you try to parse Json to Map type: it will raise exception. To fix this, just declare the result as LinkedTreeMap type. Example below:

    String nestedJSON = "{"id":"1","message":"web_didload","content":{"success":1}};
    Gson gson = new Gson();
    LinkedTreeMap result = gson.fromJson(nestedJSON , LinkedTreeMap.class);
    

提交回复
热议问题