JAvaでstringタイプ文字列をmapに変換し、値を取りやすい

1413 ワード

String array_str = "{\"24\":\"     \",\"25\":\"      \",\"10\":\"    \",\"32\":\"     \",\"20\":\"  \",\"21\":\"      \"}";

方法1:
        Gson gson = new Gson();
        Map map = new HashMap();
        map = gson.fromJson(array_str, map.getClass());
        String name = (String) map.get("10");
        System.out.println("     :"+name);

方法2:
        Gson gson = new Gson();
        Map hashMap = new HashMap();
        hashMap = gson.fromJson(array_str, new TypeToken>() {
        }.getType());
        String name = (String) hashMap.get("10");
        System.out.println(name);

リファレンス
https://www.cnblogs.com/chongyou/p/7466039.html
方法3:
com.alibaba.fastjson
String responseEntityBody = responseEntity.getBody();//{"msg":"    !","obj":{"uid":"0400022700000063-1"},"success":true}
JSONObject parseObject = JSONArray.parseObject(responseEntityBody);
JSONObject obj = parseObject.getJSONObject("obj");
String uid = obj.getString("uid");