Home > Article > Web Front-end > What is the relationship between javascript objects and json
The relationship between JavaScript objects and JSON is: JSON is the string representation of JavaScript objects. JSON uses text to represent a JavaScript object information, which is essentially a string. You can use the "JSON.parse()" method to convert from a JSON string to a js object.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
JSON is a string representation of a JS object. It uses text to represent the information of a JS object, which is essentially a string.
For example
var` `obj = {a: ``'Hello'``, b: ``'World'``}; ``//这是一个对象,注意键名也是可以使用引号包裹的 var` `json = ``'{"a": "Hello", "b": "World"}'``; ``//这是一个 JSON 字符串,本质是一个字符串
JSON and JS objects are converted to each other
To convert from JSON string to JS object, use the JSON.parse() method:
var` `obj = JSON.parse(``'{"a": "Hello", "b": "World"}'``); ``//结果是 {a: 'Hello', b: 'World'}
To convert from a JS object to a JSON string, use the JSON.stringify() method:
var` `json = JSON.stringify({a: ``'Hello'``, b: ``'World'``}); ``//结果是 '{"a": "Hello", "b": "World"}'
Related recommendations: javascript learning tutorial
The above is the detailed content of What is the relationship between javascript objects and json. For more information, please follow other related articles on the PHP Chinese website!