json 轉換成 帶泛型的對象 , 報錯問題解決

場景

當我們將json轉換成帶泛型的對象時, 再獲取該泛型屬性時會報錯
java.util.LinkedHashMap cannot be cast to xxx(你的泛型的類型)

解決辦法

1 先將該泛型屬性的值, 轉爲json字符串
2 再將該json字符串, 轉成泛型的類型

錯誤演示

// bean 類
public class HttpResult<T> {
    private boolean success;
    private T result;
    private String error;
    
    // get,set ,tostring
}
public static void main(String[] args) throws Exception {
	String json = "你的json字符串";
	// json轉bean
	ObjectMapper mapper = new ObjectMapper();
	HttpResult<QueryResult> response = mapper.readValue(stockInfoResponseJson, HttpResult.class);
	
	System.out.println(response); // 此時不會報錯
	System.out.println(response.getResult()); // 此時也不會報錯
	QueryResult queryResult = response.getResult(); // 此時會報錯
}

解決辦法演示

public static void main(String[] args) throws Exception {
	String json = "你的json字符串";
	// json轉bean
	ObjectMapper mapper = new ObjectMapper();
	HttpResult<QueryResult> response = mapper.readValue(stockInfoResponseJson, HttpResult.class);
	
	// 開始解決 報錯問題 --------------------------------------
	// 泛型 屬性 轉 json字符串
	String conStr= mapper .writeValueAsString(response.getResult());
	//  json字符串 再轉 泛型 屬性
	QueryResult queryResult = mapper.readValue(conStr, QueryResult.class);
	System.out.println(queryResult); // 此時就沒問題了
}

json & bean 互轉

上述轉換過程 , 可以使用下面工具類 JsonUtil 中的 conveterObject 方法來實現
QueryResult queryResult = JsonUtil.conveterObject(response.getResult(), QueryResult.class);

package com.nspark.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Map;

/**
 * @Description: json 工具類
 * @Author: North Spark
 */
public class JsonUtil {


    /**
     * 對象轉JSON字符串
     */
    public static String object2Json(Object obj) {
        String result = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            result = objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Function: bean 轉 map
     */
    public static Map object2Map(Object obj) {
        String object2Json = object2Json(obj);
        Map<?, ?> result = jsonToMap(object2Json);
        return result;
    }

    /**
     * JSON字符串轉Map對象
     */
    public static Map<?, ?> jsonToMap(String json) {
        return json2Object(json, Map.class);
    }

    /**
     * JSON轉Object對象
     */
    public static <T> T json2Object(String json, Class<T> cls) {
        T result = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            result = objectMapper.readValue(json, cls);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * @Process : 泛型 轉 json , json 再轉 bean
     * @Function: 泛型屬性, 轉成指定的類
     */
    public static <T> T conveterObject(Object srcObject, Class<T> destObjectType) {
        String jsonContent = object2Json(srcObject);
        return json2Object(jsonContent, destObjectType);
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章