当前位置:网站首页 / javascript / 正文
json
2019-07-17 / javascript / 929 次围观 / 0 次吐槽 /json字符串
Ø JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
Ø JSON 是轻量级的文本数据交换格式
Ø JSON 独立于语言
Ø JSON 具有自我描述性,更易理解
Json 字典
{ "firstName":"Bill" , "lastName":"Gates" }
Json数组
[ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName":"Carter" } ]
Json 字典数组
{ "employees": [ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName":"Carter" } ] }
json对象和字符串相互转换
<script type="text/javascript"> //json字符串 var str = '{"name":"tom","age":"19"}'; //json字符串转为对象,方便操作 var jsonObj = JSON.parse(str); //打印 console.log(jsonObj.name); console.log(jsonObj.age); //json转换为字符串 var jsonStr = JSON.stringify(jsonObj); console.log(jsonStr); //json字符串遍历 var strArr = '[{"name":"hacssd","age":"21"},{"name":"sdgf","age":"14"}]'; var jsonArr = JSON.parse(strArr); console.log(jsonArr); for (var i = 0; i < jsonArr.length; i++) { var o = jsonArr[i]; console.log(o); } </script>
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.