Jackson是一款为Java平台提供的一套数据处理类库工具,Jackson的主要功能是提供JSON解析和生成,另外,Jackson还提供额外的类库以支持处理Avro, CBOR, CSV, Smile, XML 和 YAML,功能十分强大,在Java中处理JSON数据非常不错。
相关的示例代码:
{ "name" : { "first" : "Joe", "last" : "Sixpack" }, "gender" : "MALE", "verified" : false, "userImage" : "Rm9vYmFyIQ==" }
public class User { public enum Gender { MALE, FEMALE }; public static class Name { private String _first, _last; public String getFirst() { return _first; } public String getLast() { return _last; } public void setFirst(String s) { _first = s; } public void setLast(String s) { _last = s; } } private Gender _gender; private Name _name; private boolean _isVerified; private byte[] _userImage; public Name getName() { return _name; } public boolean isVerified() { return _isVerified; } public Gender getGender() { return _gender; } public byte[] getUserImage() { return _userImage; } public void setName(Name n) { _name = n; } public void setVerified(boolean b) { _isVerified = b; } public void setGender(Gender g) { _gender = g; } public void setUserImage(byte[] b) { _userImage = b; } }
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally User user = mapper.readValue(new File("user.json"), User.class);
Jackson作为一款JSON处理类库工具,使用起来也非常方便。
时间: 2024-10-27 08:11:42