1. 程式人生 > >java中自定義封裝json以及和物件的轉換

java中自定義封裝json以及和物件的轉換

首先建立服務端返回的資料型別物件:

package com.studio.pojo.util;

import org.codehaus.jackson.map.ObjectMapper;

public class PlanResult {

    //定義jackson物件
    private static final ObjectMapper MAPPER = new ObjectMapper();
    //響應業務狀態
    private Integer status;
    //相應訊息
    private String msg;
    //相應中的資料
    private Object data;

    public static ObjectMapper getMAPPER() {
        return MAPPER;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

建立json和物件轉化的工具類:

package com.studio.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

/**
 * 高效todo自定義響應結構
 */
public class JsonUtils {

    //定義jackson物件
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 將物件轉化為json字串
     * @param data
     * @return
     */
    public static String ObjectToJson(Object data){
        try{
            String string = MAPPER.writeValueAsString(data);
            return string;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 將json字串轉化為物件
     * @param jsonData
     * @param beanType
     * @param <T>
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType){
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 將json字串資料轉化為pojo物件list
     * @param jsonData
     * @param beanType
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType){
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = MAPPER.readValue(jsonData, javaType);
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

測試將返回物件轉化為json字串:

package com.studio.util;

import com.studio.pojo.util.PlanResult;

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

public class test {
    public static void main(String[] args){
        PlanResult result = new PlanResult();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("username","wangtianyuan");
        resultMap.put("password","123456");
        resultMap.put("accountEmail","
[email protected]
"); result.setData(resultMap); result.setMsg("查詢成功!"); result.setStatus(1); System.out.println(JsonUtils.ObjectToJson(result)); } }

輸出結果:

{"status":1,"msg":"查詢成功!","data":{"password":"123456","accountEmail":null,"username":"wangtianyuan"}}