java对象转Json学习
json收款定制开发多用于数据存储、收款定制开发交换与传输,json相比于xml更小,更快,收款定制开发更容易解析
-
收款定制开发对象的表示方法
#javaPerson p = new Person();p.setName("张三");p.setAge(23);p.setGander("男");#javascriptvar p = { "name":"张三", "age":"23", "gender":"男"};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
-
Json收款定制开发的基本样式
Json收款定制开发的数据存储在键值对中,收款定制开发键用单引号或者双引号引起来,收款定制开发也可以不用引号,收款定制开发值的类型可以是数字、字符串、布尔值、数组、对象或者null。
json1:#键值对{ "name":"张三", "sex":"male", "age":18, "area":"北京"}json2:#数组作为value{ "data":[ { "name":"李四", "sex":"female", "age":19, "area":"上海" }, { "name":"王五", "sex":"male", "age":22, "area":深圳 } ] "dep":"开发中心", "code":105}json3:#数组作为json对象[ { "name":"张三", "sex":"male", "age":18, "area":"北京" }, { "name":"李四", "sex":"female", "age":19, "area":"上海" }]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
通过.键名、json对象[“键名”]、数组对象[索引]来获取json收款定制开发中存储的数据。
-
json和对象相互转换
常见解析器:jsonlib,,fastjson,jackson
以jackson为例,json转java对象一般步骤:
1、导入jackson相关的[jar包](链接:https://pan.baidu.com/s/1Kice6s4rjKZ3uODvmNX0ow?pwd=8pqz
提取码:8pqz)
2、创建jackson核心对象ObjectMapper
3、调用ObjectMapper的相关方法进行转换(readvalue(json字符串,class))
java对象转json一般步骤:
1、导入jackson相关jar包
2、创建jackson的核心对象ObjectMapper
3、调用ObjectMapper相关方法进行转换:
- writeValue(参数1,obj),参数1可以是三种形式:
File:将obj对象转化为json字符串并保存在指定的文件中;
Writer:将obj对象转化为json字符串,并将json数 填充到字符输出流
中;
OutputStream:将obj对象转化为Json字符串,并将json数据填充到字节输出流
中。
- xxxxxxxxxx stream { upstream dbserver { server 172.16.1.51:3306; server 172.16.1.52:3306; server 172.16.1.53:3306; server 172.16.1.54:3306; server 172.16.1.55:3306; server 172.16.1.56:3306; } server { listen 5555; proxy_pass dbserver; }}text
jackson相关注解:
1、@JsonIgnore
作用:在json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都会被这个注解影响
使用方法:标记在属性或者方法上方,表示该属性或者该方法生成json数据时不包含该属性或方法
使用场景:
实体类中基本属性字段的值都存储在快照属性字段中,因此我们可以在业务层中将快照属性字段的值赋给实体类中对应的基本属性字段。但是需要将对象作为json传输的时候,我们希望返回的json数据中不包括快照字段,这时候在实体类中快照属性上添加注解@JsonIgnore,那么最后返回的json数据将不包含快照属性字段。
public class HistoryOrderBean { //基本属性字段 private String insurantName; private String insuranceName; private String insurancePrice; private String insurancePicture; private String insuranceLimit; //快照属性字段 @JsonIgnore private String goodsInfo; //快照基本信息 @JsonIgnore private String extendsInfo; //快照扩展属性信息}#需要注意的是,如果使用的是其他json转换器,忽略字段的注解大概率会不一样,比如fastJson中忽略字段的注解是@JSONField(serialize=false),使用方法是一样的
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2、@JsonIgnoreProperties
这个注解是类注解,也就是用在java类的上面,它的作用和@JsonIgnore类似,在json序列化时将java bean中的一些属性忽略掉,同样序列化和反序列化都受这个注解的影响。
//生成json时将name和age属性过滤@JsonIgnoreProperties({"name"},{"age"}) public class user { private String name; private int age;}
- 1
- 2
- 3
- 4
- 5
- 6
3、@JsonFormat
这个注解一般用于Date类型属性上,可以指定Date类型的数据直接转化为我们想要的值,比如@JsonFormat(pattern=“yyyyMM-dd HH-mm-ss”)
4、@JsonSerialize
这个注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double类型数据时限制其后两位小数点。
5、@JsonDeserialize
这个注解和上面类似,只不过是用于反序列化
6、@Transient
标注了这个注解,ORM框架将忽略这个属性。如果一个属性并非数据库表的字段映射,就务必将这个属性标注为@Transient,否则ORM框架默认将其注解为@Basic
-
效果测试
实体类Person:
import com.fasterxml.jackson.annotation.JsonFormat;import com.fasterxml.jackson.annotation.JsonIgnore;import java.util.Date;public class Person { private String name; private String sex; private int age; //@JsonIgnore //忽略属性 @JsonFormat(pattern = "yyyy-mm-dd") private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", birthday=" + birthday + '}'; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
测试类JacksonTest:
import com.fasterxml.jackson.databind.ObjectMapper;import org.junit.Test;import java.io.File;import java.io.FileWriter;import java.util.*;public class testJackson { //Java对象转为JSON字符串 @Test public void test1() throws Exception { //1.创建Person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setSex("男"); //2.创建Jackson的核心对象 ObjectMapper ObjectMapper mapper = new ObjectMapper(); //3.转换 /* 转换方法: writeValue(参数1,obj): 参数1: File:将obj对象转换为JSON字符串,并保存到指定的文件中 Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中 OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中 writeValueAsString(obj):将对象转为json字符串 */ String json = mapper.writeValueAsString(p); //{Person{name='张三', sex='男', age=23, birthday=null} System.out.println(p); System.out.println(json);//{"name":"张三","age":23,"gender":"男"} //writeValue,将数据写到d://a.txt文件中 mapper.writeValue(new File("d://a.txt"),p); //writeValue.将数据关联到Writer中 mapper.writeValue(new FileWriter("d://b.txt"),p); } #输出结果: #Person{name='张三', sex='男', age=23, birthday=null} #{"name":"张三","sex":"男","age":23,"birthday":null} /*格式化时间对象*/ @Test public void test2() throws Exception { //1.创建Person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setSex("男"); p.setBirthday(new Date()); //2.转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(p); System.out.println(json);//{"name":"张三","age":23,"gender":"男","birthday":1530958029263} //{"name":"张三","sex":"男","age":23,"birthday":"2020-30-21"} } #输出结果: #{"name":"张三","sex":"男","age":23,"birthday":"2022-43-12"} /*多个封装对象转json数组对象*/ @Test public void test3() throws Exception { //1.创建Person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setSex("男"); p.setBirthday(new Date()); Person p1 = new Person(); p1.setName("李四"); p1.setAge(19); p1.setSex("女"); p1.setBirthday(new Date()); Person p2 = new Person(); p2.setName("王五"); p2.setAge(22); p2.setSex("男"); p2.setBirthday(new Date()); //创建List集合 List<Person> ps = new ArrayList<Person>(); ps.add(p); ps.add(p1); ps.add(p2); //2.转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(ps); // [{},{},{}] //[{"name":"张三","sex":"男","age":23,"birthday":"2020-33-21"},{"name":"张三","sex":"男","age":23,"birthday":"2020-33-21"},{"name":"张三","sex":"男","age":23,"birthday":"2020-33-21"}] System.out.println(json); } #输出结果: #[{"name":"张三","sex":"男","age":23,"birthday":"2022-44-12"},{"name":"李 #四","sex":"女","age":19,"birthday":"2022-44-12"},{"name":"王五","sex":"男","age":22,"birthday":"2022-44- #12"}] /*map对象转json对象*/ @Test public void test4() throws Exception { //1.创建map对象 Map<String,Object> map = new HashMap<String,Object>(); map.put("name","张三"); map.put("age",23); map.put("gender","男"); //2.转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(map); //{"name":"张三","age":23,"gender":"男"} System.out.println(json);//{"gender":"男","name":"张三","age":23} } #输出结果: #{"gender":"男","name":"张三","age":23} //演示 JSON字符串转为Java对象 @Test public void test5() throws Exception { //1.初始化JSON字符串 String json = "{\"sex\":\"男\",\"name\":\"张三\",\"age\":23}"; //2.创建ObjectMapper对象 ObjectMapper mapper = new ObjectMapper(); //3.转换为Java对象 Person对象 Person person = mapper.readValue(json, Person.class); System.out.println(person);//Person{name='张三', sex='男', age=23, birthday=null} } #输出结果: #Person{name='张三', sex='男', age=23, birthday=null}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141