json分享

JSON是什么?

JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is derived from the object literals of JavaScript

    JSON是JavaScript Object Notation的简称,JSON易于访问且结构性强,用来存储信息(数据)。

JSON例子

    和 XML 一样,JSON 也是基于纯文本的数据格式。由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,您可以用 JSON 传输一个简单的 String,Number,Boolean,也可以传输一个数组,或者一个复杂的 Object 对象。

    1、Object

var person = {
    "name" : "Lily",
    "gender" : "female",
    "age" : "24"
}

     我们创建了一个对象,可以通过person访问。在这个对象中我们声明3组数据,这些数据使用“名称:值”对的集合,以逗号分隔。我们可以直接通过属性的名称访问,例如:

console.log("姓名:"+person.name+" 性别:"+person.gender+" 年龄:"+person.age);

    2、Array

var articles = [{

    "id" : 111,

    "article" : "html",

    "author":"Lily"

},

{

    "id" : 222,

    "article" : "css",

    "author":"Jack"

}

]

    对于Array格式的JSON数据,我们可以通过索引来访问:

console.log(articles[0].id);  //111
console.log(articles[1].author);   //Jack

其他JSON例子:

{
    "africaLagos": [
      {
        "from": -377711769600000,
        "to": -1588464816000,
        "dst": false,
        "offset": 816,
        "name": "LMT"
      },
      {
        "from": -1588464816000,
        "to": 253402300799000,
        "dst": false,
        "offset": 3600,
        "name": "WAT"
      }
    ]
}

Facebook JSON File

{
   "data": [
      {
         "id": "X999_Y999",
         "from": {
            "name": "Tom Brady", "id": "X12"
         },
         "message": "Looking forward to 2010!",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.facebook.com/X999/posts/Y999"
            },
            {
               "name": "Like",
               "link": "http://www.facebook.com/X999/posts/Y999"
            }
         ],
         "type": "status",
         "created_time": "2010-08-02T21:27:44+0000",
         "updated_time": "2010-08-02T21:27:44+0000"
      },
      {
         "id": "X998_Y998",
         "from": {
            "name": "Peyton Manning", "id": "X18"
         },
         "message": "Where's my contract?",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.facebook.com/X998/posts/Y998"
            },
            {
               "name": "Like",
               "link": "http://www.facebook.com/X998/posts/Y998"
            }
         ],
         "type": "status",
         "created_time": "2010-08-02T21:27:44+0000",
         "updated_time": "2010-08-02T21:27:44+0000"
      }
   ]
}

Colors JSON File

{
    "colorsArray":[{
            "colorName":"red",
            "hexValue":"#f00"
        },
        {
            "colorName":"green",
            "hexValue":"#0f0"
        },
        {
            "colorName":"blue",
            "hexValue":"#00f"
        },
        {
            "colorName":"cyan",
            "hexValue":"#0ff"
        },
        {
            "colorName":"magenta",
            "hexValue":"#f0f"
        },
        {
            "colorName":"yellow",
            "hexValue":"#ff0"
        },
        {
            "colorName":"black",
            "hexValue":"#000"
        }
    ]
}

iPhone Menu JSON 

{
    "menu": {
        "header": "xProgress SVG Viewer",
        "items": [
            {
                "id": "Open"
            },
            {
                "id": "OpenNew",
                "label": "Open New"
            },
            null,
            {
                "id": "ZoomIn",
                "label": "Zoom In"
            },
            {
                "id": "ZoomOut",
                "label": "Zoom Out"
            },
            {
                "id": "OriginalView",
                "label": "Original View"
            },
            null,
            {
                "id": "Quality"
            },
            {
                "id": "Pause"
            },
            {
                "id": "Mute"
            },
            null,
            {
                "id": "Find",
                "label": "Find..."
            },
            {
                "id": "FindAgain",
                "label": "Find Again"
            },
            {
                "id": "Copy"
            },
            {
                "id": "CopyAgain",
                "label": "Copy Again"
            },
            {
                "id": "CopySVG",
                "label": "Copy SVG"
            },
            {
                "id": "ViewSVG",
                "label": "View SVG"
            },
            {
                "id": "ViewSource",
                "label": "View Source"
            },
            {
                "id": "SaveAs",
                "label": "Save As"
            },
            null,
            {
                "id": "Help"
            },
            {
                "id": "About",
                "label": "About xProgress CVG Viewer..."
            } 
        ]
    }
}

Customer Form JSON 

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
 }

Products Database JSON File

{
        "name":"Product",
        "properties":
        {
                "id":
                {
                        "type":"number",
                        "description":"Product identifier",
                        "required":true
                },
                "name":
                {
                        "description":"Name of the product",
                        "type":"string",
                        "required":true
                },
                "price":
                {
                        "type":"number",
                        "minimum":0,
                        "required":true
                },
                "tags":
                {
                        "type":"array",
                        "items":
                        {
                                "type":"string"
                        }
                }
        }
}
  • 空格(空格、tab、换行、新行或回车)对JSON没有影响。它也可以压缩,对数据没有影响。
  • 对象字面名称必须小写(例如, null, false, true 等)。
  • 把所有名称/值放在引号中。
  • 在使用你的JSON之前,先验证是否是有效的。 http://www.jsonlint.com
  • JSON默认Unicode编码为UTF-8(查看所有应用程序的媒体类型http://www.iana.org/assignments/media-types/application/index.html)
  • JSON文本的MIME媒体类型为application /json

JSON解析

    JSON.parse(): 将JSON字符串转换成对象或数组,若失败则报错。

var str = '{"name":"Lily","age":"18"}';
console.log(typeof str);   // string

var obj = JSON.parse(str);
console.log(typeof obj);   //object

 

    JSON.stringify(): 用于将一个对象解析成字符串。

var obj = {"name":"Lily","age":"18"};
console.log(typeof obj); // object

var str= JSON.stringify(obj);
console.log(typeof str);  //string

JSON转义字符

下面的字符需要转义:引号", 斜杠/,反斜杠\, 换行n,回车r, tab t

引号"

var obj = {"name":"\"\/\\\n\r\tLily","age":"23"};
console.log(obj.name);
//output: "/\

    Lily

JSON工具

  1. BE JSON  JSON在线校验  

  2. SO JSON  在线JSON校验

  3. JSON Parser Online  简单的字符串解析和JS Eval

  4. JSONLint   非常流行的在线JSON校验

  5. Online JSON Tree Viewer  加载JSON文件,展示数据的树结构

  6. JSON Editor – XML to JSON

  7. XML to JSON and JSON to XML

参考文章:http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/

http://www.sitepoint.com/basics-json-syntax-tips/

 

posted on 2015-01-21 17:03  加油_linda  阅读(477)  评论(0编辑  收藏  举报