写在最前
JSON的先容就不外多先容了,可以直接看w3c对JSON的简短先容 W3C-JSON 。 这篇文章先容的Jackson是JSON的一个类库 .
Jackson
- com.fasterxml.jackson.core jackson-databind 2.11.0
复制代码- 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格式化
- 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
- public class Example { @JsonIgnore private String color; private String value;}
复制代码 {“value”:“abc”}
- @JsonIgnoreProperties({"value"})public class Example { private String color; private String value;}
复制代码 {“color”:“red”}
- try { ObjectMapper mapper = new ObjectMapper(); String str = "[{"color":"red", "value":"kateliuyi"}, {"color":"blue", "value":"nas"}]"; List 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)]
- try { ObjectMapper mapper = new ObjectMapper(); String string = "{"name":"kateliuyi", "value":"abc"}"; Map map = mapper.readValue(string, Map.class); System.out.println(map);}catch (Exception e) { e.printStackTrace();}
复制代码 {name=kateliuyi, value=abc}
来源:https://blog.csdn.net/csder_xj/article/details/111941375
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |