Object 和 JSON 区别联系

JavaScript                                                       

Object-based

JavaScript is almost entirely object-based.

Object                                                              

name

Object property names are string keys.

syntax

dot notation (obj.x = 10)

bracket notation (obj['x'] = 10)

value

value can be a string in double quotes, or a number, or true or false or null, or an object or an array.

enumerated

for...in

JSON (JavaScript Object Notation)                 

structures

A collection of name/value pairs.  {}

An ordered list of values.      []

JSON 和 object 区别                                            

var o = {"a":1,"b":2};
console.dir(typeof(o));// object

var a = [{"a":1},{"b":2}];
console.dir(typeof(a));// object

json 是对 object 的 描述

json 不是对象

json object 是 object

json 字符串 是 string

JSON object 和 JSON string 区别                        

JSON object 有懒模式:可以缺省name的"" ,该name 不会被解析 。

var a = "abc";
var o = {a:1};//不标准写法;a不会被abc代替。
console.dir(o.a);//1
console.dir(o.abc);//undefined

JSON string 格式必须是标准格式,否则不能解析。

var o = {a:1};
var ostr = JSON.stringify(o);
console.dir(ostr); // {"a":1}     标准
                   // "{\"a\":1}"
console.dir(JSON.parse('{"a":1}'));   // Object
console.dir(JSON.parse("{\"a\":1}")); // Object
console.dir(JSON.parse("{a:1}"));     // SyntaxError

 通过 Function 将懒模式的字符串转换为 JSON object

var ostr = "{a:1,b:2}";
var o = (new Function("return "+ostr))();
console.dir(o); // Object
console.dir(JSON.stringify(o)); // {"a":1,"b":2}    

 

转载于:https://www.cnblogs.com/zno2/p/4672904.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值