Json数据格式
json是一种与编程语言无关的数据交换的格式。使用ajax进行前后台数据交换,移动端与服务端的数据交换。
json的格式与解析
json有两种格式:
1)对象格式:{"key1":obj,"key2":obj,"key3":obj...}
2)数组格式:[obj,obj,obj...]
//以user对象为例
{"username":"lisi","age":22,"password":"123456","addr":"长沙"}
[{"pid":10,"pname":"华为p20"},{...},{...}]
注意:
- 对象格式和数组格式可以相互嵌套
- json的key是字符串 json的value是Object
//js里书写json对象
<script>
var person = {"firstname":"汪","lastname":"思源","age":22}
var persons = [{"firstname":"张","lastname":"三","age":18},{"firstname":"李","lastname":"四","age":82}];
var json = {
"baobao":[
{"name":"小双","age":28,"addr":"扬州"},
{"name":"建宁","age":18,"addr":"紫禁城"},
{"name":"阿珂","age":19,"addr":"山西"}
]
}
// 取person的lastname值
alert(person.lastname);
// 取persons=李
alert(persons[1].firstname);
// 取阿珂
alert(json.baobao[2].name);
</script>
Gson
// TODO
FastJson
//TODO
网友评论