Java高性能的JSON处理 Jackson
openkk
13年前
<p>Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好。</p> <ul> <li>Streaming (reading, writing)</li> <li>FAST (measured to be faster than any other Java json parser and data binder)</li> <li>Powerful (full data binding for common JDK classes as well as any Java bean class, Collection, Map or Enum)</li> <li>Zero-dependency (does not rely on other packages beyond JDK)</li> <li>Open Source (LGPL or AL)</li> <li>Fully conformant</li> <li>Extremely configurable</li> </ul> <p>示例:</p> <pre class="code-java">{ <span class="code-quote">"name"</span> : { <span class="code-quote">"first"</span> : <span class="code-quote">"Joe"</span>, <span class="code-quote">"last"</span> : <span class="code-quote">"Sixpack"</span> }, <span class="code-quote">"gender"</span> : <span class="code-quote">"MALE"</span>, <span class="code-quote">"verified"</span> : <span class="code-keyword">false</span>, <span class="code-quote">"userImage"</span> : <span class="code-quote">"Rm9vYmFyIQ=="</span> }</pre> <pre class="brush:java; toolbar: true; auto-links: false;">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; } }</pre> <pre class="brush:java; toolbar: true; auto-links: false;">ObjectMapper mapper = new ObjectMapper(); // can reuse, share globallyUser user = mapper.readValue(new File("user.json"), User.class);</pre> <p>从上面例子可看到,使用也是非常的方便。</p> <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1324131120328" target="_blank">http://www.open-open.com/lib/view/home/1324131120328</a></p>