JAvaでのカスタムパッケージjsonおよびオブジェクトとの変換

3493 ワード

まず、サービス側から返されるデータ型オブジェクトを作成します.
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 
     * @return
     */
    public static  T jsonToPojo(String jsonData, Class 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 
     * @return
     */
    public static  List jsonToList(String jsonData, Class beanType){
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List 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 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"}}