JavaScript Patterns 3.5 JSON

JSON: JavaScript
Object Notation

{"name": "value", "some": [1, 2, 3]} 

The only syntax difference between JSON and the object literal is that
property names need to be wrapped in quotes to be valid JSON.
In object literals the quotes are required only when the property names are not
valid identifiers, for example, they have spaces {"first name": "Dave"}.

In JSON strings you cannot use functions or regular expression
literals.

Working with JSON

JSON.parse()

use the JSON.parse()method, which is part of the language since ES5 and is
natively provided by the JavaScript engines in modern browsers.

For older JavaScript engines, you can use the JSON.org library (http://www.json.org/json2.js)
to gain access to the  JSON object and its methods.


// an input JSON string

var jstr = ‘{"mykey": "my value"}‘;

// antipattern

var data = eval(‘(‘ + jstr + ‘)‘);

// preferred

var data = JSON.parse(jstr);

console.log(data.mykey); // "my value"

using YUI3


// an input JSON string

var jstr = ‘{"mykey": "my value"}‘;

// parse the string and turn it into an object

// using a YUI instance

YUI().use(‘json-parse‘, function (Y) {

var data = Y.JSON.parse(jstr);

console.log(data.mykey); // "my value"

});

using JQuery


// an input JSON string

var jstr = ‘{"mykey": "my value"}‘;

var data = jQuery.parseJSON(jstr);

console.log(data.mykey); // "my value" 

JSON.stringify()


var dog = {

name: "Fido",

dob: new Date(),

legs: [1, 2, 3, 4]

};

var jsonstr = JSON.stringify(dog);

// jsonstr is now:

// {"name":"Fido","dob":"2010-04-11T22:36:22.436Z","legs":[1,2,3,4]}

JavaScript Patterns 3.5 JSON

时间: 2024-10-12 12:13:34

JavaScript Patterns 3.5 JSON的相关文章

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

Gets a length property containing the number of arguments the function expects: function func(a, b, c) {} console.log(func.length); // 3 var myFunc = function () { // serialize the arguments object as a JSON string and use that string as a key in you

JavaScript Patterns 2.7 Avoiding Implied Typecasting

Dealing with == and === false == 0 or "" == 0 return true. always use the === and !== operators that check both the values and the type of the expressions you compare: var zero = 0; if (zero === false) { // not executing because zero is 0, not f

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

JavaScript Patterns 3.7 Primitive Wrappers

Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 100; console.log(typeof n); // "number" // a Number object var nobj = new Number(100); console.log(typeof nobj); // "object"  One reason

JavaScript Patterns 2 Essentials - Writing Maintainable Code

Revisiting the code after some time has passed requires: ? Time to relearn and understand the problem ? Time to understand the code that is supposed to solve the problem As the application matures, many other things happen that require your code to b

JavaScript Patterns 7.1 Singleton

7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =

JavaScript Patterns 2.12 Writing API Docs

Free and open source tools for doc generation: the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/) and YUIDoc (http://yuilibrary.com/projects/yuidoc). Process of generating API documentation ? Writing specially formatted code blocks ? Running

JavaScript Patterns 2.11 Writing Comments

Document all functions, their arguments and return values, and also any interesting or unusual algorithm or technique. Think of the comments as hints to the future readers of the code; the readers need to understand what your code does without readin

JavaScript Patterns 2.2 - Minimizing Globals

Access a global variable in a browser environment: myglobal = "hello"; // antipattern console.log(myglobal); // "hello" console.log(window.myglobal); // "hello" console.log(window["myglobal"]); // "hello"