简体   繁体   English

JSON.parse()如何工作?

[英]How does JSON.parse() work?

I have not worked too much on javascript. 我没有在javascript上工作​​太多。 And, I need to parse a JSON string. 而且,我需要解析一个JSON字符串。 So, I want to know what exactly JSON.parse does. 所以,我想知道JSON.parse究竟是做什么的。 For example : If I assign a json string to a variable like this, 例如:如果我将json字符串分配给这样的变量,

var ab = {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}};

Now when I print 'ab', I get an object. 现在,当我打印'ab'时,我得到一个对象。

Similarly when I do this : 同样当我这样做时:

var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';
var rs = JSON.parse(pq);

The 'rs' is the same object as 'ab'. 'rs'与'ab'是同一个对象。 So what is the difference in two approaches and what does JSON.parse did differently ? 那么两种方法的区别是什么,JSON.parse有什么不同呢?

This might be a silly question. 这可能是一个愚蠢的问题。 But it would be helpful if anybody can explain this. 但如果有人能够解释这一点会有所帮助。

Thanks. 谢谢。

Here is my explanation with a jsfiddle . 这是我对jsfiddle的解释。

//this is already a valid javascript object
//no need for you to use JSON.parse()
var obj1 = {"name":"abcd", "details":"1234"};
console.log(obj1);

//assume you want to pass a json* in your code with an ajax request
//you will receive a string formatted like a javascript object
var str1 = '{"name":"abcd", "details":"1234"}';
console.log(str1);

//in your code you probably want to treat it as an object
//so in order to do so you will use JSON.parse(), which will
//parse the string into a javascript object
var obj2 = JSON.parse(str1);
console.log(obj2);

JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. JSON或JavaScript Object Notation是用于构造数据的最小可读格式。 It is used primarily to transmit data between a server and web application, as an alternative to XML. 它主要用于在服务器和Web应用程序之间传输数据,作为XML的替代方案。

A Javascript object is a data type in Javascript - it's have property and value pair as you define in your first example. Javascript对象是Javascript中的数据类型 - 它具有您在第一个示例中定义的属性和值对。

var ab = {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}};

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) 现在什么是Json:JSON字符串是一种数据交换格式 - 它只不过是一堆特定方式的字符(为了让不同的程序相互通信)

var pq = '{"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}}';

so it's is a String With json Format. 所以这是一个带有json格式的字符串。

and at last JSON.parse() Returns the Object corresponding to the given JSON text. 最后JSON.parse()返回与给定JSON文本对应的Object。

Your 'ab' variable isn't a string, it is a proper javascript object, since you used the {} around it. 你的'ab'变量不是字符串,它是一个合适的javascript对象,因为你在它周围使用了{}。 If you encased the whole thing in "" then it would be a string and would print out as a single line. 如果你将整个东西包裹在“”中那么它将是一个字符串并将打印成一行。

Data Type!! 数据类型!! That is the answer. 这就是答案。 In this case, ab is an object while pq is a string (vaguely speaking). 在这种情况下, ab是一个对象,pq是一个字符串(含糊地说)。 Print is just an operation that displays 'anything' as a string. 打印只是一个将“任何”显示为字符串的操作。 However, you have to look at the two differently. 但是,你必须以不同的方式看待这两者。 String itself is an object which has properties and methods associated with it. 字符串本身是一个对象,它具有与之关联的属性和方法。 In this case, pq is like an object which has a value: {"name":"abcd", "details":{"address":"pqrst", "Phone":1234567890}} and for example, it has a property called length whose value is 66. But ab is an object and you can look at name and details as its properties. 在这种情况下, pq就像一个具有值的对象: {“name”:“abcd”,“details”:{“address”:“pqrst”,“Phone”:1234567890}}例如,它有一个名为length的属性,其值为66.但是ab是一个对象,您可以将名称详细信息视为其属性。

What JSON.parse() did differently was that, it parsed (converted) that string into an object. JSON.parse()做的不同之处在于它将该字符串解析(转换)为对象。 Not all strings can be parsed into objects. 并非所有字符串都可以解析为对象。 Try passing {"name":"abc" and JSON.parse will throw an exception. 尝试传递{“name”:“abc” ,JSON.parse将抛出异常。

Before parsing, pq did not have any property name . 在解析之前, pq没有任何属性名称 If you did something like pq.name , it'll return you undefined . 如果你做了类似pq.name的事情,那么它将返回你未定义的内容 But when you parsed it using JSON.parse() then rs.name will return the string "abcd". 但是当你使用JSON.parse()解析它时, rs.name将返回字符串“abcd”。 But rs will not have the property length anymore because it is not a string. 但是rs不再具有属性长度 ,因为它不是字符串。 If you tried rs.length then you'll get a value undefined. 如果您尝试了rs.length,那么您将得到一个未定义的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM