个人总结的jackson简单用法,希望对大家有多帮助!xml解析失败,若有朋友看到万望指点一二!
引入相关jar包,如:jackson-all-1.7.6.jar
一个简单的bean类:
package com.Jtianlin.test;
public class User {
private String username;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
java对象到json字符串:
package com.Jtianlin.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
public class BeanToJson {
public static void main(String[] args) throws Exception {
//将对象转换成json字符串
User user = new User();
user.setAge(10);
user.setUsername("jzx");
ObjectMapper objectMapper = new ObjectMapper();
String userJson = objectMapper.writeValueAsString(user);
System.out.println(userJson);
//打印:{"username":"jzx","age":10}
//将list集合对象转换成json字符串
User user1 = new User();
user1.setAge(11);
user1.setUsername("sss");
User user2 = new User();
user2.setAge(12);
user2.setUsername("hsj");
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
String userListJson = objectMapper.writeValueAsString(userList);
System.out.println(userListJson);
//打印结果:[{"username":"sss","age":11},{"username":"hsj","age":12}]
//将map集合转换成json字符串。
Map<String,User> userMap = new HashMap<String,User>();
userMap.put("user1", user1);
userMap.put("user2", user2);
String userMapJson = objectMapper.writeValueAsString(userMap);
System.out.println(userMapJson);
//打印结果:{"user2":{"username":"hsj","age":12},"user1":{"username":"sss","age":11}}
}
}
一个含java对象集合变量的类,用于json字符串数组到对象集合:
package com.Jtianlin.test;
import java.util.List;
public class ListBean {
List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
package com.Jtianlin.test;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonToBeans {
public static void main(String[] args) throws Exception {
//将json字符串转换成java对象
String userJson = "{\"username\":\"jzx\",\"age\":10}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(userJson,User.class);
System.out.println(user+":"+user.getUsername()+","+ user.getAge());
//打印结果:com.Jtianlin.test.User@c2a132:jzx,10
//将json字符串数组转换成java对象list集合。直接转换成list集合较为复杂,本例通过一个过渡对象
//该对象里面存有一个list的java对象集合。通过转换成对象来实现集合对象的转换。
String userListJson = "{\"userList\":[{\"username\":\"sss\",\"age\":11},{\"username\":\"hsj\",\"age\":12}]}";
ListBean listBeans = objectMapper.readValue(userListJson, ListBean.class);
System.out.println(listBeans.getUserList());
//打印结果:[com.Jtianlin.test.User@1e0bc08, com.Jtianlin.test.User@158b649]
//将json字符串转换成map集合,可以通过键来取得值。
String mapJson = "{\"username\":\"hsj\",\"age\":12}";
Map userMap = objectMapper.readValue(mapJson, Map.class);
System.out.println(userMap+":" + userMap.get("username")+","+userMap.get("age") );
//打印结果:{username=hsj, age=12}:hsj,12
//将json字符串数组转换成Map的list集合。
String json ="[{\"username\":\"sss\",\"age\":11},{\"username\":\"hsj\",\"age\":12}]}";
List<Map> list = objectMapper.readValue(json, List.class);
System.out.println(list.get(0).get("username"));
//打印结果:sss
}
}