JSONUtil工具类

import java.util.HashMap;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class JSONUtils {
    /**
     * Bean对象转JSON
     * 
     * @param object
     * @param dataFormatString
     * @return
     */
    public static String beanToJson(Object object, String dataFormatString) {
        if (object != null) {
            if (StringUtils.isEmpty(dataFormatString)) {
                return JSONObject.toJSONString(object);
            }
            return JSON.toJSONStringWithDateFormat(object, dataFormatString);
        } else {
            return null;
        }
    }

    /**
     * Bean对象转JSON
     * 
     * @param object
     * @return
     */
    public static String beanToJson(Object object) {
        if (object != null) {
            return JSON.toJSONString(object);
        } else {
            return null;
        }
    }

    /**
     * String转JSON字符串
     * 
     * @param key
     * @param value
     * @return
     */
    public static String stringToJsonByFastjson(String key, String value) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
            return null;
        }
        Map map = new HashMap();
        map.put(key, value);
        return beanToJson(map, null);
    }

    /**
     * 将json字符串转换成对象
     * 
     * @param json
     * @param clazz
     * @return
     */
    public static Object jsonToBean(String json, Object clazz) {
        if (StringUtils.isEmpty(json) || clazz == null) {
            return null;
        }
        return JSON.parseObject(json, clazz.getClass());
    }

    /**
     * json字符串转map
     * 
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map jsonToMap(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        return JSON.parseObject(json, Map.class);
    }
}

测试:
JSON字符串转map

public static void main(String[] args) {
        String jsonStr = "{'userName':'huangbaokang','password':'123456'}";
        Map map = JSONUtils.jsonToMap(jsonStr);
        System.out.println(map.get("userName"));
    }

输出:huangbaokang

JSON字符串转对象:
新建User类

public class User {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}
public static void main(String[] args) {
        String jsonStr = "{'userName':'huangbaokang','password':'123456'}";
        User user = (User) JSONUtils.jsonToBean(jsonStr, new User());
        System.out.println("用户名="+user.getUserName()+" 密码="+user.getPassword());
    }

测试:输出用户名=huangbaokang 密码=123456

对象转JSON

public static void main(String[] args) {
        User user = new User();
        user.setUserName("huangbaokang");
        user.setPassword("123456");
        String result = JSONUtils.beanToJson(user);
        System.out.println(result);

    }

测试输出:{“password”:”123456”,”userName”:”huangbaokang”}

在这里工具类应用的jar为阿里巴巴的fastjson,在lib中加入相关jar。

专业墙纸贴纸厨房用具装饰出售,本人网店经营

博客对你有用记得访问下哦,增加下访问量,如有需要可以下单购买哦^_^。https://item.taobao.com/item.htm?id=569617707364

你可能感兴趣的:(json)