Jackson簡單教程
阿新 • • 發佈:2021-01-11
寫在最前
JSON的介紹就不過多介紹了,可以直接看w3c對JSON的簡短介紹 W3C-JSON 。 這篇文章介紹的Jackson是JSON的一個類庫 .
Jackson
- 引入依賴,我這用最新的版本
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.0</version> </dependency>
- JAVA物件轉換JSON;
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
Example exa = create();
/*把java物件內的屬性寫到example.txt檔案*/
mapper.writeValue(new File("e:\\example.txt"), exa);
}catch (Exception e) {
e.printStackTrace ();
}
}
static Example create (){
Example exa = new Example();
exa.setValue("values;");
exa.setColor("red");
return exa;
}
example.txt 檔案的內容 {“color”:“red”,“value”:“values;”}
writeValue不止可以轉換成檔案,它有很多過載的方法
try {
ObjectMapper mapper = new ObjectMapper() ;
// 以JSON字串的形式輸出
String string = mapper.writeValueAsString(create());
System.out.println(string);
}catch (Exception e) {
e.printStackTrace();
}
{“color”:“red”,“value”:“values;”}
如果你的JSON字串較長,那麼他的格式將顯得非常的亂,你可以指定讓他更美化的輸出
try {
ObjectMapper mapper = new ObjectMapper();
String beautifulJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(create());
System.out.println(beautifulJson);
}catch (Exception e) {
e.printStackTrace();
}
{
“color” : “red”,
“value” : “values;”
}
當然你也可以用一些線上的工具來完成這個工作 JSON格式化
- JSON轉換為JAVA物件
ObjectMapper mapper = new ObjectMapper();
Example e = mapper.readValue(new File("e:\\example.txt"), Example.class);
System.out.println(e);
Example(color=red, value=values;)
try {
ObjectMapper mapper = new ObjectMapper();
String str = "{\"value\":\"kateliuyi\",\"color\":\"blue\"}";
Example e = mapper.readValue(str, Example.class);
System.out.println(e);
} catch (Exception e){
e.printStackTrace();
}
Example(color=blue, value=kateliuyi)
- 指定JSON的欄位命名
在本篇文章,Example物件轉換為JSON輸出的欄位名是color、value(是以javabean的屬性來決定的), 如果想改變JSON輸出的欄位命名,需要用到@JsonProperty
註解
@JsonProperty("changeColor")
private String color;
private String value;
Example(changeColor=blue, value=kateliuyi)
- 忽略空的欄位 -
@JsonInclude
預設情況下輸出的JSON是包含空字串的 , 如果想忽略空,需要用到@JsonInclude
註解
@JsonInclude
應用到類, 表示這個類的所有欄位都忽略空
try {
ObjectMapper mapper = new ObjectMapper();
// 以JSON字串的形式輸出
String string = mapper.writeValueAsString(create());
System.out.println(string);
}catch (Exception e) {
e.printStackTrace();
}
static Example create (){
Example exa = new Example();
/*exa.setValue("values;");(*/
exa.setColor(null);
return exa;
}
{}
@JsonInclude
應用到欄位, 只忽略此欄位
public class Example {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String color;
private String value;
}
static Example create (){
Example exa = new Example();
exa.setValue(null);
exa.setColor(null);
return exa;
}
{“value”:null}
- 忽略指定欄位 -
@JsonIgnore
@JsonIgnoreProperties
@JsonIgnore
public class Example {
@JsonIgnore
private String color;
private String value;
}
{“value”:“abc”}
@JsonIgnoreProperties
@JsonIgnoreProperties({"value"})
public class Example {
private String color;
private String value;
}
{“color”:“red”}
- 對集合的操作
java.util.List
try {
ObjectMapper mapper = new ObjectMapper();
String str = "[{\"color\":\"red\", \"value\":\"kateliuyi\"}, {\"color\":\"blue\", \"value\":\"nas\"}]";
List<Example> list = Arrays.asList(mapper.readValue(str, Example[].class));
System.out.println(list);
}catch (Exception e) {
e.printStackTrace();
}
[Example(color=red, value=kateliuyi), Example(color=blue, value=nas)]
java.util.Map
try {
ObjectMapper mapper = new ObjectMapper();
String string = "{\"name\":\"kateliuyi\", \"value\":\"abc\"}";
Map<String, String> map = mapper.readValue(string, Map.class);
System.out.println(map);
}catch (Exception e) {
e.printStackTrace();
}
{name=kateliuyi, value=abc}