Home >Web Front-end >Front-end Q&A >What is the relationship between javascript objects and json

What is the relationship between javascript objects and json

WBOY
WBOYOriginal
2022-03-10 16:08:542728browse

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.

What is the relationship between javascript objects and json

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What is the relationship between JavaScript objects and json

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!

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