Home >Web Front-end >JS Tutorial >What is the difference between json and javascript objects

What is the difference between json and javascript objects

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-16 14:57:384698browse

The difference between json and javascript objects is that the text of the JS argument is directly parsed by the script engine, while the JSON text, if it is to be converted into a JS object, is handed over to the eval function for processing.

What is the difference between json and javascript objects

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

//js对象的字面量表示法: 
var people1={ 
name:'hehe', 
age:18 
}; 

//json的格式是: 
var people1={ 
"name":'hehe', 
"age":18 
};

The two are similar in that they both appear to be data, and they happen to be text; the difference is that the text of JS literals is directly parsed by the script engine, while the text of JSON If you want to convert it into a JS object, it is handled by the eval function. Then, how to understand the JSON text depends on this function, not the script engine, because the processing of the two is not at the same level at all.

var obj={width:100,height:200}

This is not called JSON, and JSON is just a data format, not a specific instance object. But many people regard such JS objects as JSON.

1. JSON (JavaScript Object Notation) is a lightweight data exchange format. JSON format data is mainly used for cross-platform exchange of data.

2. But JSON and JavaScript do have origins. It can be said that this data format evolved from JavaScript objects and is a subset of JavaScript. JSON itself means JavaScript Object Notation, which uses strict JavaScript object notation to represent structured data.

3. It is a strict js object format. The attribute name of JSON must have double quotes. If the value is a string, it must also be double quotes;

4. JSON is just A data format (or data form). Data format is actually a specification. Format, form, and specification cannot be used to store data. We cannot call the following objects JSON, for example:

        var obj2={}; //这只是JS对象
        var obj3={width:100,height:200};
        /*这跟JSON就更不沾边了,只是JS的对象 */

        var obj4={'width':100,'height':200};
        /*这跟JSON就更不沾边了,只是JS的对象 */

        var obj5={"width":100,"height":200,"name":"rose"}; 
        /*我们可以把这个称做:JSON格式的JavaScript对象 */

        var str1='{"width":100,"height":200,"name":"rose"}';
        /*我们可以把这个称做:JSON格式的字符串 */

        var a=[
        {"width":100,"height":200,"name":"rose"},
        {"width":100,"height":200,"name":"rose"},
        {"width":100,"height":200,"name":"rose"},
        ];
        /*这个叫JSON格式的数组,是JSON的稍复杂一点的形式 */

        var str2 = '['+
        '{"width":100,"height":200,"name":"rose"},'
        +'{"width":100,"height":200,"name":"rose"},'
        +'{"width":100,"height":200,"name":"rose"},'
        +']';
        /*  这个叫稍复杂一点的JSON格式的字符串  */

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is the difference between json and javascript objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn