最近需要解析json数据,以前用json-lib觉得还是可以的,就是有时候不是很方便,然后使用了以下jackson json
结果发现真的很方便。
我需要将list对象转化成json对象,也需要将json 数组转化成List对象。
jackson json 官方地址:http://jackson.codehaus.org/
jackson json 有三种方式转换json数据的操作,
流模式,树模式,数据绑定模式。分别用于不同的环境下,对于我的需求数据绑定模式很简单就可以实现。
user对象代码:
package jackson;
import java.io.Serializable;
/**
*
* @author alaric
*
*/
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
private Integer sex;
private Integer age;
private String brithday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
}
result对象代码:
package jackson;
import java.io.Serializable;
import java.util.List;
public class Result implements Serializable {
/**
*
*/
private static final long serialVersionUID = 9121346318498657366L;
private Integer msgCode;
private String message;
private List<User> list;
public Integer getMsgCode() {
return msgCode;
}
public void setMsgCode(Integer msgCode) {
this.msgCode = msgCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
@Override
public String toString() {
return "{msgCode:"+msgCode+" message:"+message+" list:"+list.toString()+"}";
}
}
测试代码:
package jackson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JacksonTest {
public static Result json2Obj(String json){
try {
ObjectMapper mapper = new ObjectMapper();
Result result = mapper.readValue(json, Result.class);
return result;
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String obj2Json(Result result){
try {
ObjectMapper mapper= new ObjectMapper();
String json = mapper.writeValueAsString(result);
return json ;
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Result getData(){
Result result = new Result();
result.setMessage("请求成功");
result.setMsgCode(1);
List<User> list = new ArrayList<>();
User user1= new User();
user1.setAge(25);
user1.setBrithday("1986-06-26");
user1.setId(1);
user1.setPassword("3424242");
user1.setSex(2);
user1.setUsername("zhangsan");
list.add(user1);
result.setMessage("请求成功");
result.setMsgCode(1);
User user2= new User();
user2.setAge(25);
user2.setBrithday("1986-06-26");
user2.setId(1);
user2.setPassword("3424242");
user2.setSex(2);
user2.setUsername("lisi");
list.add(user2);
result.setList(list);
return result;
}
public static void main(String[] args) {
Result result = getData();
String json = obj2Json(result);
System.out.println(json);
result = json2Obj(json);
List <User> list = result.getList();
for(User user:list){
System.out.println(user.getUsername());
}
System.out.println(result.toString());
}
}