zno2

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}    

 

posted on 2016-08-05 18:48  zno2  阅读(1018)  评论(0编辑  收藏  举报

导航