GSON parse generic Json Array

后端 未结 3 1062
暖寄归人
暖寄归人 2020-12-21 23:18

My question is very similiar to this: Unable to parse Json array using Gson

But I cann\'t get the answer from it. The answer from above link:

public         


        
3条回答
  •  感情败类
    2020-12-21 23:54

    You can use this method in order to parse generic json string to map

        public Map getMapFromJson(String jsonString) {
        Map map = new HashMap<>();
        try {
            JSONObject object = new JSONObject(jsonString);
            Iterator iterator = object.keys();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                if(!key.isEmpty() && !object.getString(key).isEmpty()){
                    map.put(key, object.getString(key));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }
    

提交回复
热议问题