源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JSON与js对象序列化实例详解

  • 时间:2020-03-17 11:19 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JSON与js对象序列化实例详解
本文实例讲述了JSON与js对象序列化。分享给大家供大家参考,具体如下: JavaScript对象表示法(JavaScript Object Notation,简称JSON)是一种轻量级的数据交换格式,它基于js字面量表示法,是js的一个子集。虽然是一个js的子集但是他与语言无关,它可以用于在现在所有的编程语言编写的应用程序之间进行数据交换。是一种文本格式,比较容易读写。 JSON是一个容纳“名/值”对的无序集合,名字可以是任意字符串,值可以使任意的JSON类型的值。大多数编程语言都有被映射为JSON的数据类型,比 如对象(object),字典(dictionary),哈希表(hash map),关联数组(associative array)等。 JSON有六种类型的值:对象,数组,字符串,数字,布尔值和特殊值null。
console.log(JSON.parse('5')); // 5
console.log(JSON.parse(5)); // 5
console.log(JSON.parse('true')); // true
console.log(JSON.parse(true)); // true
console.log(JSON.parse('"hello"')); // "hello"
console.log(JSON.parse("hello")); // 报错 因为hello不是JSON字符串
console.log(JSON.parse('null')); // null
console.log(JSON.parse(null)); // null
console.log(JSON.parse(undefined)); // 报错 因为JSON不能表示undefined换用null代替

[b]JSON的结构[/b] JSON具有两种结构:对象,数组 对象结构以”{”大括号开始,以”}”大括号结束。中间部分由0或多个以”,”分隔的”key(关键字)/value(值)”对构成,关键字字符串和值之间以”:”分隔,语法结构如代码。
{
  key1:value1,
  key2:value2,
  ...
}

例如:
{
  "name": "hum",
  "age": 26,
  "sex": 1,
  "love": [
    "swing",
    "jump"
  ],
  "birthday": "1988-01-12"
}

[b]NOTE:[/b] 在js中表示JSON字符串时最好在外面加上单引号。 如下:
[url=http://tools.jb51.net/code/json]http://tools.jb51.net/code/json[/url] [b]JSON[/b][b]在线格式化工具: [/b][url=http://tools.jb51.net/code/jsonformat]http://tools.jb51.net/code/jsonformat[/url] [b]在线XML/JSON互相转换工具: [/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url] [b]json[/b][b]代码在线格式化/美化/压缩/编辑/转换工具: [/b][url=http://tools.jb51.net/code/jsoncodeformat]http://tools.jb51.net/code/jsoncodeformat[/url] [b]在线json压缩/转义工具:[/b] [url=http://tools.jb51.net/code/json_yasuo_trans]http://tools.jb51.net/code/json_yasuo_trans[/url] [b]C语言风格/HTML/CSS/json代码格式化美化工具: [/b][url=http://tools.jb51.net/code/ccode_html_css_json]http://tools.jb51.net/code/ccode_html_css_json[/url] 更多关于JavaScript相关内容可查看本站专题:《[url=http://www.1sucai.cn/Special/313.htm]JavaScript中json操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/472.htm]JavaScript查找算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/439.htm]JavaScript错误与调试技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/297.htm]JavaScript数据结构与算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/281.htm]JavaScript遍历算法与技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/119.htm]JavaScript数学运算用法总结[/url]》 希望本文所述对大家JavaScript程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部