Mais conteúdo relacionado Semelhante a SoftLeader Jackson Training (20) SoftLeader Jackson Training2. Main Projects
• jackson-core
• jackson-annotations
• jackson-databind
• Third-party
Guava
Hibernate
HPPC
Joda
org.json
JSR-310 ( Java 8 Date )
JSR-353 ( Java JSON API )
JDK7
3. Maven
• groupId : com.fasterxml.jackson.core
• artifactId:
jackson-core
jackson-databind
jackson-annotations
• version
now is 2.4.0
4. What is JSON format
{
"id": 1,
"account": "admin",
"name": "Administrator"
}
if in XML , too complex and bigger size.
<root>
<user id= "1">
<account>admin</account>
<name>Administrator</name>
</user>
</root>
5. JSON is simpler
• Property
{"attribute1 ": "string ", "attribute2 ": number}
• List
{"attribute3 ": ["Apple", "Banana", "Cherry"]}
• Map/Object
{"attribute4":{"field1":"value1", "field2":"value2"}}
7. Data Binding
ObjectMapper objectMapper = new ObjectMapper();
mapper.readValue( src, MyBean.class);
MyBean myBean = new MyBean();
mapper.writeValue(dst, myBean);
• File
• InputStream / OutputStream
• Reader / Writer
• String
8. Java Mapping
JSON Type Java Type
object LinkedHashMap<String, Object>
array ArrayList<Object>
string String
number ( no fraction) Integer, Long, BigInteger
number ( fraction ) Double ( BigDecimal )
true/false Boolean
null null
9. Basic Annotations
• @JsonProperty 對應到 json 的屬性值
• @JsonIgnore 忽略輸入與輸出 // property level
• @JsonIgnoreProperties({.., ..}) // class level
• @JsonIgnoreProperties(ignoreUnknown=true)