Difference and Comparison between JSON and JS Objects

  • 2021-07-24 09:56:07
  • OfStack

What is JSON? What is the difference between JSON and JavaScript objects? ) How to convert an JS object to an JSON string and an JSON string to an JavaScript object?

JSON (JavaScript Object Notation) is a simple data format that is lighter than xml. JSON is an JavaScript native format, which means that no special API or toolkit is required to process JSON data in JavaScript. The rule of JSON is simple: The object is an unordered collection of "name/value" pairs. 1 object begins with "{" (left parenthesis) and ends with "}" (right parenthesis). Each "name" is followed by a ":" (colon); "Name/value" pairs are separated by "," (commas).

It is a strict js object format, and the attribute name of JSON must have double quotation marks, and if the value is a string, it must also be double quotation marks;

JSON is only one data format (or data form);


<script>
var obj2={};// It's just JS Object 
var obj3={width:100,height:200};/* This is similar to JSON It's even more irrelevant , It's just JS Adj.   Object  */
var obj4={'width':100,'height':200};/* This is similar to JSON It's even more irrelevant , It's just JS Object of  */
var obj5={"width":100,"height":200,"name":"rose"}; /* We can call this: JSON Format JavaScript Object  */
var str1='{"width":100,"height":200,"name":"rose"}';/* We can call this: JSON Format string  */
var a=[
 {"width":100,"height":200,"name":"rose"},
 {"width":100,"height":200,"name":"rose"},
 {"width":100,"height":200,"name":"rose"},
 ];
 /* This is called JSON An array in the format, which is JSON Slightly complicated 1 Form of point  */
var str2='['+
 '{"width":100,"height":200,"name":"rose"},'+
 '{"width":100,"height":200,"name":"rose"},'+
 '{"width":100,"height":200,"name":"rose"},'+
 ']' ;
 /*  This is called a little complicated 1 Point JSON Format string  */ 
</script>

JSON and JS Object Difference Comparison Table

区别

Json

Javascript对象

含义

仅仅是1种数据格式

表示类的实例

传输

可以跨平台数据传输,速度快

不能传输

表现

1,键值对方式,键必须加引号

2,值不能是方法函数,不能是undefined/NaN

1,键值对方式,键不加引号

2,值可以是函数、对象、字符串、数字、boolean 等

相互转换

Json转化为js对象:

1,JSON.parse(jsonstring);
(不兼容ie7)

2,Jsobj=eval("("+jsonstring+")");

(兼容所有浏览器,但不安全,会执行json里面的表达式?)

Js对象转换为Json:

JSON.stringify(jsobj);(不兼容ie7)

其他

调用json官网的js,实现parse 和 stringify 在各个浏览器的兼容:

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

To sum up, you can understand that JSON is a data format under JS, which belongs to JS, and can directly use JS built-in API when processing JSON data


Related articles: