JSON.parse()如何工作? [英] How does JSON.parse() work?
问题描述
我在JavaScript方面的工作还不是很多.而且,我需要解析一个JSON字符串.所以,我想知道JSON.parse到底是做什么的.例如 : 如果我将json字符串分配给这样的变量,
I have not worked too much on javascript. And, I need to parse a JSON string. So, I want to know what exactly JSON.parse does. For example : If I assign a json string to a variable like this,
var ab = {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}};
现在,当我打印'ab'时,我得到一个对象.
Now when I print 'ab', I get an object.
类似地,当我这样做时:
Similarly when I do this :
var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';
var rs = JSON.parse(pq);
"rs"与"ab"是同一对象.那么这两种方法有什么区别,JSON.parse有何不同之处?
The 'rs' is the same object as 'ab'. So what is the difference in two approaches and what does JSON.parse did differently ?
这可能是一个愚蠢的问题.但是如果有人可以解释这将是有帮助的.
This might be a silly question. But it would be helpful if anybody can explain this.
谢谢.
推荐答案
Javascript对象是Javascript中的数据类型-它具有在第一个示例中定义的属性和值对.
A Javascript object is a data type in Javascript - it's have property and value pair as you define in your first example.
var ab = {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}};
现在Json是什么:JSON字符串是一种数据交换格式-它不过是一堆以特定方式格式化的字符(以使不同的程序相互通信)
Now What is Json : A JSON string is a data interchange format - it is nothing more than a bunch of characters formatted a particular way (in order for different programs to communicate with each other)
var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';
所以它是一个带有json格式的字符串.
so it's is a String With json Format.
,最后是JSON.parse()
返回与给定JSON文本相对应的Object.
and at last JSON.parse()
Returns the Object corresponding to the given JSON text.
这篇关于JSON.parse()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!