Java JSON處理庫Jackson

Jackson是一款爲Java平臺提供的一套數據處理類庫工具,Jackson的主要功能是提供JSON解析和生成。另外,Jackson還提供額外的類庫以支持處理Avro, CBOR, CSV, Smile, XML 和 YAML。功能十分強大,在Java中處理JSON數據很不錯。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處理庫的工具,這也很容易使用。app

版權聲明:本文博客原創文章,博客,未經贊成,不得轉載。工具

相關文章
相關標籤/搜索