json 的基礎知識。
- array and list –> wrap in [ ]
- object –> wrap in { }
jackson 需要注意
- 需要序列化的pojo 的屬性必須具有setter和getter方法,或者是public的。
java pojo –> json
一個簡單的例子:
下面的例子是採用databind 的方式生成 json 串的。
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(person);
System.out.println(json);
序列化的配置:
//you can indent the json output. just use during testing.
objectMapper.configure(INDENT_OUTPUT, true);
//provide date formatter
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd_HH/mm/ss");
objectMapper.setDateFormat(simpleDateFormat);
//change the name of json properties, use some display strategy
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
//handle null value
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
使用tree api 手動生成或者解析
使用tree api 手動構建一個json 串。
//用來手動創建節點。
JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance;
//jsonfactory 就是定義輸入和輸出的位置的
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator = jsonFactory.createGenerator(System.out);
//首先創建一個root 節點
ObjectNode root = jsonNodeFactory.objectNode();
root.put("username", "zhangsan");
root.put("age", 14);
root.put("friend", "wangwu");
ArrayNode arrayNode = jsonNodeFactory.arrayNode();
arrayNode.add(1);
arrayNode.add(2);
arrayNode.add(3);
arrayNode.add(4);
//然後創建一個 array 節點
root.put("ages",arrayNode);
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
objectMapper.writeTree(jsonGenerator, root);
使用tree api 解析一個json 串
String json = "{\"username\":\"zhangsan\",\"age\":14,\"friend\":\"wangwu\",\"ages\":[1,2,3,4]}";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
//解析基本的數據類型。
String username = jsonNode.findValue("username").asText();
int age = jsonNode.findValue("age").asInt();
//解析數組
JsonNode arrayNode = jsonNode.findValue("ages");
Iterator<JsonNode> iterator = arrayNode.iterator();
List<Integer> ages = new ArrayList<Integer>();
while (iterator.hasNext()) {
int temAge = iterator.next().asInt();
ages.add(temAge);
}
Person temPerson = new Person(username, age, new Date());
System.out.println(temPerson);
System.out.println(ages);
使用高級api data-bind 來解析數據。
對於開發者來首,面對的最多的是 java pojo。那麼data-bind api 就是負責 將一個pojo對象解析成一個json串或者從一個json串生成一個pojo實例。
data-bind 解析和生成的例子。
//解析。
String json = "{\"username\":\"zhangsan\",\"age\":14}";
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(json, Person.class);
System.out.println(person);
//生成
Person zhangsanPerson = new Person("zhangsan", 16, new Date());
ObjectMapper objectMapper1 = new ObjectMapper();
String jsonResult = objectMapper1.writeValueAsString(zhangsanPerson);
System.out.println(jsonResult);
如果pojo的某些屬性不想生成
- 你可以使用類級別的註解(@JsonIgnoreProperties),來屏蔽一些字段。就像:
@JsonIgnoreProperties({"username"})
public class Person {
....
}
在這個例子中,字段username就被屏蔽了。從一個json串生成時,username將會是null。從一個Person 實例生成json 時,將不會有username的信息。
結果就像下面一樣:
Person{username='null', age=14, birthday=null}
{"age":16,"birthday":1468377397642,"firent":null}
- 使用字段級別的註解(@)
public class Person {
@JsonIgnore
private String username;
}
結果也像上面一樣。
一些註解的含義
- @JsonProperty-This annotation is used to mark a method as a getter or setter for a property.
- @JsonCreator-This annotation is used the define constructors that are used to create java objects from json string.
- @JsonAnyGetter and @JsonAnySetter - This annotations are used to mark methods that set or read fields that are not handled by any other java property.
對於3 。來說是這個樣子的,就是如果Jackson 在遇到pojo沒有定義的屬性時,就調用有3 註解的標籤。來存放這些屬性。
更詳細的註解請看詳細的註解說明