Json格式String类型字符串转为Map工具类

package agriculture_implement.util;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.util.Map;

public class JsontoMap {

    public static Map<String, Object> json2map(String str_json){
        Map<String, Object> res = null;
        try {
            Gson gson = new Gson();
            res = gson.fromJson(str_json, new TypeToken<Map<String, Object>>() {
            }.getType());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return res;
    }

}

 需要引入的包:

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

 

posted @ 2018-03-10 08:39  谁将新樽辞旧月,今月曾经照古人  阅读(453)  评论(0编辑  收藏  举报