json字符串轉(zhuǎn)list對象

String serverUrl = PathUtils.zqBaseUrl + "/theme/getFinishedList.do?";
String params = "type=" + type;
String result = "";
result = HttpUtil.getRequestPost(serverUrl, params);//json字符串
List<CbxyCell_temporary> list = new ArrayList <CbxyCell_temporary();  
list=JsonUtil.toObject(result,new TypeReference<List<CbxyCell_temporary>>()  { });
json工具類
package net.radar.util;

import java.io.IOException;

import net.radar.entity.LightEquipment;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonUtil {
    
    
    private static final String JACKSON_OBJECT_MAPPER_BEAN_NAME = "jacksonObjectMapper";// jackson ObjectMapper Bean名稱

    public static ObjectMapper getMapper() {
        return (ObjectMapper) SpringUtil.getBean(JACKSON_OBJECT_MAPPER_BEAN_NAME);
    }
    
    // 將對象轉(zhuǎn)換為JSON字符串
    public static String toJson(Object object) {
        ObjectMapper mapper = getMapper();
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    // 將JSON字符串轉(zhuǎn)換為對象
    public static <T> T toObject(String json, Class<T> clazz) {
        ObjectMapper mapper = getMapper();
        try {
            return mapper.readValue(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    // 將JSON字符串轉(zhuǎn)換為對象
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static <T> T toObject(String json, TypeReference typeReference) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = getMapper();
        return mapper.readValue(json, typeReference);
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容