1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>fastjson</artifactId> 4 <version>1.2.7</version> 5 </dependency>
首先引入相關jar包,json
假設有一個實體類Userspa
public class User{ private int id; private String name; }
而後經過fastJson進行類型轉換code
public static void main(String[] args){ String userString = "{"id":1,"name","lz"}"; JSONObject userJson = JSONObject.parseObject(userString); User user = JSON.toJavaObject(userJson,User.class); }