IDEA Springboot Use Gson

1 添加maven依赖

<dependency>  
        <groupId>com.google.code.gson</groupId>  
        <artifactId>gson</artifactId>  
        <version>2.8.0</version>  
</dependency>  

2 使用Gson

    public static void test(){
        //定义变量
        String jsonString =
                "{" +
                "'A':'a'," +
                "'B':'b'," +
                "'C':'c'," +
                "'D':'d'," +
                "'E':'e'," +
                "'F':'f'," +
                "'G':'g'," +
                "'H':'h'," +
                "'over':'over'" +
                "}";
        Gson gson = new Gson();
        //gson 转换为map
        Map<String, String> retMap = gson.fromJson(jsonString,
                new TypeToken<Map<String, String>>(){}.getType());
        //遍历
        for (Map.Entry<String, String> entry : retMap.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
    }


猜你喜欢

转载自blog.csdn.net/qq_28197211/article/details/80455014
今日推荐