fastJson 解析、序列化及反序列化

一、环境准备:使用maven特性在pom.xml中导入fastjson的依赖包

fastJson 解析、序列化及反序列化

 

 

 

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
</dependency>

二、序列化

1.创建实体类(根据json的key值创建对象属性)

package cn.xiaobing.pojo;

/**创建实体类ResultJson,属性与提供jsonString中的key保持一致
 * @author Administrator
 */
public class ResultJson {
    private String status;
    private String message;
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

    public ResultJson() {
        super();
    }
    public ResultJson(String status, String message) {
        super();
        this.status = status;
        this.message = message;
    }
    @Override
    public String toString() {
        return "ResultJson [status=" + status + ", message=" + message + "]";
    }
}

2.利用fastJson实现序列化(ResultJson对象转化为json字符串)

package cn.xiaobing.json;

import com.alibaba.fastjson.JSONObject;

import cn.xiaobing.pojo.ResultJson;

public class jsonDemo02 {
    /**将对象序列化成json
     * @return jsonString
     */
    public static String entityToJsonString () {
        //构造器实例化对象
        ResultJson resutlJson = new ResultJson("2","登录失败,密码错误!");
        //利用fastjson中JSONObject类的toJSONString方法,实现将对象ResultJson转化为jsonString
//        String jsonString = JSONObject.toJSONString(resutlJson);
        //如果需要美化输出json格式 
        String jsonString = JSONObject.toJSONString(resutlJson,true);
        return jsonString;
    }
    public static void main(String[] args) {
        String jsonString = entityToJsonString();
        System.out.println("ResultJson序列化json字符串:"+"\n"+jsonString);
    }
}

fastJson 解析、序列化及反序列化

 

 

三、反序列化

1.提供json字符串如:

{"status":"1","message":"登录成功"}

{"status":"1","message":"登录成功","data":{"name":"Tom","age":"18","phone":"18888888888"}}

2.修改实体类对象(新增json多层数据结构)

package cn.xiaobing.pojo;

import java.util.Map;

/**创建实体类ResultJson,属性与提供jsonString中的key保持一致
 * @author Administrator
 */
public class ResultJson {
    private String status;
    private String message;
    private Map<String,String> datas;
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, String> getDatas() {
        return datas;
    }
    public void setDatas(Map<String, String> datas) {
        this.datas = datas;
    }
    public ResultJson() {
        super();
    }
    public ResultJson(String status, String message, Map<String, String> datas) {
        super();
        this.status = status;
        this.message = message;
        this.datas = datas;
    }
    @Override
    public String toString() {
        return "ResultJson [status=" + status + ", message=" + message + ", datas=" + datas + "]";
    }

}

3.利用fastJson实现json字符串的反序列化

package cn.xiaobing.json;

import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import cn.xiaobing.pojo.ResultJson;

/**利用fastJson实现json字符串的反序列化
 * @author Administrator
 */
public class jsonDemo01 {
    //提供json字符串{"status":"1","message":"登录成功"}
    static String jsonString = "{\"status\":\"1\",\"message\":\"登录成功\"}";
    static String multilayerJsonString = "{\"status\":\"1\",\"message\":\"登录成功\",\"datas\":{\"name\":\"Tom\",\"age\":\"18\",\"phone\":\"18888888888\"}}";
    /**
     * 利用fastJson实现json字符串反序列化为对象
     */
    public static ResultJson jsonStringToEntity(String jsonString) {

        //创建实体类 resultJson,利用fastjson中JSONObject类的parseObject方法,实现将jsonString转化为对象ResultJson
        ResultJson resultJson = JSONObject.parseObject(jsonString, ResultJson.class);
        return resultJson;
    }
    /**
     * 利用fastJson实现json字符串保存Map中
     * @param jsonString
     */
    public static Map<String, String> jsonStringToMapping(String jsonString) {
        //将json字符串封装到Map
        Map<String, String> map = (Map<String, String>) JSONObject.parse(jsonString);
        return map;
    }
    /**
     * 利用fastJson实现json(多层嵌套json)字符串反序列化为对象
     */
    public static ResultJson multilayerJsonStringToEntity(String multilayerJsonString) {

        //创建实体类 resultJson,利用fastjson中JSONObject类的parseObject方法,实现将jsonString转化为对象ResultJson
        ResultJson resultJson = JSONObject.parseObject(multilayerJsonString, ResultJson.class);
        return resultJson;
    }

    public static void main(String[] args) {
        ResultJson resultJson1 = jsonStringToEntity(jsonString);
        System.out.println("json转换成对象输出:"+"{status:"+resultJson1.getStatus()+",message:"+resultJson1.getMessage()+"}");
        Map<String, String> map1 = jsonStringToMapping(jsonString);
        System.out.println("json转换成Map输出:"+"{status:"+map1.get("status")+",message:"+map1.get("message")+"}");
        ResultJson resultJson2 = multilayerJsonStringToEntity(multilayerJsonString);
        Map<String, String> map2 = resultJson2.getDatas();
        System.out.println("json(多层嵌套json)转换成对象输出:"+"{status:"+resultJson2.getStatus()+",message:"+resultJson2.getMessage()+
                    " ,datas:{name:"+map2.get("name")+",age:"+map2.get("age")+",phone:"+map2.get("phone")+"}}");
    }
}

 

fastJson 解析、序列化及反序列化

上一篇:webServer_soap——xml解析


下一篇:2019前端面试系列——CSS面试题