Array JSON

W3Schools.com

Tool: Online jsonviewer

JSON: JavaScript Object Notation.

JSON is a syntax for storing and exchanging data.

JSON Objects

JSON objects are written inside curly braces.

{"name":"John", "age":25, "japanese":false}

JSON Arrays

JSON arrays are written inside square brackets.

"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter","lastName":"Jones"}
]

JSON Uses JavaScript Syntax

var employees = [
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter","lastName": "Jones"}
]; 

Be accessed:

// returns John Doe
employees[0].firstName + " " + employees[0].lastName;

// returns John Doe
employees[0]["firstName"] + " " + employees[0]["lastName"];

Be modified

employees[0].firstName = "Gilbert";

employees[0]["firstName"] = "Gilbert";

Object From String: Create a JavaScript string containing JSON syntax

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

The JavaScript function JSON.parse(text) can be used to convert a JSON text into a JavaScript object:

// Normal browsers
var obj = JSON.parse(text);
// Older browsers
var obj = eval ("(" + text + ")");
// Output
obj.employees[1].firstName + " " + obj.employees[1].lastName

以上です

posted @ 2015-12-16 18:59  提佰萬  阅读(285)  评论(0编辑  收藏  举报