一、数据格式
- 数字:1
- 字符:“a”
- 对象:{ }
- 一维数组:[ ]
- 二维数组:[ [ ] ]
- 对象数组:[ { } ]
一条json数据由上面基本类型组成,比如:
{
"name":"smith",
"age":28,
"sex":"男",
"major":["计算机","心理学"],
"school":{
"sname":"广州大学",
"address":"大学城外环西路108号"
}
}
二、转换
- 添加依赖
<!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
- Java类转json字符串
User user = new User();
String UserJson = JSON.toJSONString(user);
- json字符串转java对象
String jsonStr1 = "{'password':'123456','username':'dmego'}";
User user = JSON.parseObject(jsonStr1, User.class);