首页 > 代码库 > JavaScript高级程序设计之JSON

JavaScript高级程序设计之JSON

IE8以下请求助神之Douglas Crockford:https://github.com/douglascrockford/json-js

JSON是一种格式化的字符串,特别适合在网络上传输,由Douglas Crockford发明。

JSON语法可以表示三种类型的值:

    简单值:字符串、数值、布尔值和null

    对象

    数组

特别说明:JSON属性名必须加双引号;而JavaScript对象的属性如果是合法的标示符则不用加双引号。

// 一个javascript对象
var conference = {  
    Conference: "Future Marketing",   
    Address: "Shanghai",  
    Members:[  
        {  
            name: "Bob",  
            age: 32,  
            company: "Oracle",  
            enginner: true  
        },  
        {  
            name: "John",  
            age: 30,  
            compancy: "Google",  
            enginner: false  
        }  
    ]
};

var jsontext = JSON.stringify(conference, ["Conference", "Address"]);
// string: {"Conference":"Future Marketing","Address":"Shanghai"}

var anotherObj = JSON.parse(jsontext, function (key, value) {
    if (key === "Conference") {
        return "a conference";
    } else {
        return value;
    }
});
// object: { Conference="a conference", Address="Shanghai"}