JSON objects and JavaScript objects directly amount of difference - the difference

 JSON objects and objects directly amount JS

In the work which we always hear people say can convert the data to JSON objects, or to convert JSON object to a string that kind of thing, here is a specific description of the JSON.

 JSON objects are not JavaScript object literal (Object Literals)

Many people mistakenly believe that is the object of the JSON literal (object Literals) JavaScript them, and the reason is very simple, it is because their syntax is very similar, but in the ECMA clearly illustrates. JSON is just a data exchange language, we will only use it in the context of its time string called JSON.

Serialization and de-serialization

When two (or server, language, etc.) require interactive communication, because they tend to use string string string in many languages ​​analytical methods are similar. Complex data structures often need to use, and by various brackets {}, parentheses (), called the brackets <> to compose and spaces, the string is just good character in accordance with specification requirements.

To this end, we have to describe these complex data structure as a string string, set standards and rules of grammar. JSON just one syntax that describes objects, arrays, strings, numbers, Boolean and null string in the context, and then through the inter-program transmission, and deserialized into the required format.

Common popular interactive data format YAML, XMLand JSONare commonly used data interchange format.

Literal
reference Mozilla Developer Center in a few words, for your reference:

  1. They are fixed values, not variables, so you from "literally" Understanding script. (Literals)
  2. String literal is a double quotation mark ( ") or a single quote ( ') surrounded by zero or more characters. (Strings Literals)
  3. Object literal by braces ({}) enclose the attribute name of zero or more objects - value pairs. (Object Literals)

When will become JSON

JSON is designed to describe data exchange format, he also has its own grammar, the syntax is a subset of JavaScript.
{ "Prop": "val" } This statement may be the JavaScript object literal string JSON also possible, depending on what context to use it, if it is used in a string context (with single or double quotation marks, or text file read) from, then it is JSON string, if it is used in a context object literal, then it is the object literal.

E.g:

// This is JSON string 
var foo = '{ "prop": "Val"}' ; 

// this is the object literal 
var bar = { "prop": "Val"};

But also to pay attention to, JSON has a very strict syntax in string contexts { "prop": "val"} is a legitimate JSON, but {prop: "val"} and { 'prop': 'val'} does not legitimate of. All attribute name and its value must be incorporated in double quotation marks, not to use single quotes.

JS among the JSON object

Currently, JSON objects has become JS among a built-in objects, there are two static methods: JSON.parse and JSON.stringify.

JSON.parse main JSON string to be deserialized into objects, JSON.stringify object to serialize JSON string. Older versions of browsers do not support this object, but you can achieve the same function by json2.js.

E.g:

// This is JSON string, such character string information from the acquired AJAX 
var my_json_string = '{ "prop": "Val"}'; 

// string deserialized into objects 
var = my_obj the JSON.parse (my_json_string); 

Alert (my_obj.prop == 'val'); // tips true, and the same imaginary! 

// string object sequence into JSON 
var my_other_json_string = JSON.stringify (my_obj);

  

Guess you like

Origin www.cnblogs.com/zhaohongcheng/p/11124717.html