zoukankan      html  css  js  c++  java
  • 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}    
  • 相关阅读:
    第60届IMO 第5题
    第31届IMO 第2题
    洛谷【P1595 信封问题】 题解
    洛谷【P2022 有趣的数】 题解
    洛谷【P5004 专心OI
    04-----jQuery的属性操作
    03-----jQuery动画效果
    02-----jQuery的选择器
    01-----jQuery介绍
    17-----案例
  • 原文地址:https://www.cnblogs.com/zno2/p/4672904.html
Copyright © 2011-2022 走看看