JSON和Object互轉

json轉javaBean

jackson 的 ObjectMapper

User user = MAPPER.readValue(jsonStr, User.class);

json 轉 數組 User[]

jackson 的 ObjectMapper

	ArrayType arrayType = MAPPER.getTypeFactory().constructArrayType(User.class);
	User[] users = MAPPER.readValue(jsonStr, arrayType);

json 轉 list List<User>

jackson 的 ObjectMapper

	CollectionType collectionType = MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, User.class);
	ArrayList<GroupUser> groupUsers = MAPPER.readValue(jsonStr, collectionType);

Object 轉json

jackson 的 ObjectMapper

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