Object in JavaScript versus JSON

  • 2020-07-21 06:46:09
  • OfStack

Introduction to the

JSON, or JavaScript Object Natation, is a lightweight data exchange format that is ideal for server interactions with JavaScript.
JSON is a data exchange format that, like XML and YAML1, is a way to transfer structured information between different languages. On the other hand, the javascript object is a data type in the javascript language, like arrays, C++ classes, and structs in PHP.

Define JSON and javascript objects

When an object is defined in the javascript program, the property name of the object may or may not be quoted. If the property name contains special characters (such as! ", if, etc.), you must use double quotation marks.
When defining JSON, attribute names must be in double quotes.

Code examples:

1. Define the javascript object


var obj={name:"tudouya","sex":"man"};  # Two properties can be in double quotes or not
var obj={"!":"hello world"};  # Attribute names must be quoted when they contain special characters

2. Define JSON string

var jsonString={"name":"tudouya"};  # define JSON Must be in double quotation marks

The javascript object is converted to JSON

1.javascript object converted to JSON

We can convert the javascript object to JSON using javascript's built-in function, which is JSON.stringify ().
Code examples:


var obj={name:"tudouya",sex:"man"};
var jsonObj=JSON.stringify(obj);
console.log(jsonObj);
## The output result is: {"name":"tudouya","sex":"man"}

There is one thing to note when converting an javascript object to JSON:
If the object contains properties with values of functions and dates, JSON ignores the properties with values of functions and converts the properties with values of dates to strings.
Code examples:

var obj={
 name:"tudouya",
 birthday:new Date(),
 action:function (){
  document.write("walk");
 }
};
var jsonObj=JSON.stringify(obj);
console.log(jsonObj);
## The output result is: {"name":"tudouya","birthday":"2014-08-12T10:05:00.497Z"}

JSON is resolved in javascript

In the old version of JS, the eval() function was usually used to parse JSON, but ECMAScript5 gives us a new function to parse ES74en.parse ().

This function is easy to use, you can try it yourself. When the function is applied to an JSON string, the JSON is converted to an object of javascript, that is, when the type of the function is viewed with the typeof operator, the value returned is Object.
It is also important to note that this function has not been supported since ECMAScript5 and may not be supported on older browsers. The solution is to load an js file that implements the function, json2.js. If you are using the jQuery framework, jQuery.parseJSON (), this function calls the JSON.parse () method.
The analysis of JSON using eval() will be recorded after further study.

A very important concept

As a beginner, you often hear people say "JSON object", but there is no such thing as "JSON object". The real representation of JSON is a string.


Related articles: