Java json字符串轉Object

1:導入依賴,使用的是阿里巴巴的fastjson依賴

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>

 

2:使用 JSONObject.parseObject(String text , Class<T> clazz),舉個栗子:

        MessageModel messageModel = JSONObject.parseObject(message, MessageModel.class);

 

notice1:message 必須爲JSON格式字符串,否則在轉化過程中會報異常

 

3:API源碼

    public static <T> T parseObject(String text, Class<T> clazz) {
        return parseObject(text, clazz, new Feature[0]);
    }

 

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