1:導入依賴,使用的是阿里巴巴的fastjson依賴json
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency>
2:使用 JSONObject.parseObject(String text , Class<T> clazz),舉個栗子:spa
MessageModel messageModel = JSONObject.parseObject(message, MessageModel.class);
notice1:message 必須爲JSON格式字符串,不然在轉化過程當中會報異常code
3:API源碼blog
public static <T> T parseObject(String text, Class<T> clazz) { return parseObject(text, clazz, new Feature[0]); }