JSON概念:JavaScriptObjectNotation。JavaScript对象表示法
由于其语法简单,层次结构鲜明,现多用于作为数据载体,在网络中进行数据传输
<script>
var json={
"name":"zhangsan",
"age":23,
"city":"北京"
}
</script>
json的基础语法
JSON数据和Java对象的转换
private int id;
private String name;
private String pass;
public String toString() {
return "user2{" +
"id=" + id +
", name='" + name + '\'' +
", pass='" + pass + '\'' +
'}';
}
user2 u = new user2(1,"张三","123456");
//Java对象转JSON
String json = JSON.toJSONString(u);
System.out.println(json);
//输出结果:{"id":1,"name":"张三","pass":"123456"}
//JSON字符串转Java对象
String json2 = "{\"id\":2,\"name\":\"李四\",\"pass\":\"123456789\"}";
user2 u2 = JSON.parseObject(json2,user2.class);
System.out.println(u2);
//输出结果:user2{id=2, name='李四', pass='123456789'}