JSON.parse()如何做到这一点?

时间:2021-08-07 20:16:39

So... I am trying to use JavaScript to parse an object returned from an PHP file. The relevant code looks like this:

所以...我正在尝试使用JavaScript来解析从PHP文件返回的对象。相关代码如下所示:

var name = document.getElementBId("name").value;
var XHR = new XMLHttpRequest();
XHR.open("GET', 'lookup.php?name=' + name, true);

XHR.onreadystatechange = function (){
    try{
        alert("Attempting to parse");
        if ((XHR.readyState === 4) && (XHR.status === 200)) {
            alert("Parsing");
            var jsonresponse = JSON.parse(XHR.responseText);
            alert("Is this being skipped?");
        ....

Here's the php being returned:

这是返回的php:

{
  "name": ABC Elementary, 
  "addr": 3000 County Road 29 Alberta AL 36720-2817, 
  "county": Wilcox, "district": 6;
}

This program is supposed to submit a school's name for a math tournament and use the rest of that info to display the school's address, county, and district once its name has been selected. I picked ABC Elementary as the debugger because it's easy to type.

该计划应该为数学锦标赛提交学校名称,并在其名称被选中后使用其余信息显示学校的地址,县和地区。我选择了ABC Elementary作为调试器,因为它很容易输入。

Unfortunately, the script won't proceed past this point. I'm getting an alert from the alert("Parsing"), but not from alert("Is this being skipped?"). I'm also not getting anything past that point within the XML.onreadystatechange(), although I am still getting alerts from outside the block.

不幸的是,脚本不会超过这一点。我从警报(“解析”)收到警报,但没有从警报(“这是否被跳过?”)获得警报。我在XML.onreadystatechange()中也没有得到任何东西,尽管我仍然从块外获取警报。

My guess is that it has something to do with how JSON.parse() handles data, or at least it has to do with that line.

我的猜测是它与JSON.parse()处理数据的方式有关,或者至少与该行有关。

Also, I know that other people have asked about this, and I have been looking for these answers, but nobody seems interested in knowing what exactly JSON.parse() is doing to that data. If anyone could enlighten me, I would be very grateful.

此外,我知道其他人已经问过这个,我一直在寻找这些答案,但似乎没有人知道JSON.parse()究竟对这些数据做了什么。如果有人能够启发我,我将非常感激。

2 个解决方案

#1


0  

From my understanding, JSON.parse() takes a string value, a stringified object and parses it.

根据我的理解,JSON.parse()接受一个字符串值,一个字符串化的对象并解析它。

For JSON.parse() to work for your case, we want the object returned to be:

要使JSON.parse()适用于您的案例,我们希望返回的对象为:

{"name": "ABC Elementary", "addr": "3000 County Road 29 Alberta AL 36720-2817", "county": "Wilcox", "district": "6"}

...BUT we want that wrapped in a string as the input for parse.

...但我们希望将包裹在字符串中的内容作为解析的输入。

Add this line to your console and press enter and you will see the result of the parse:

将此行添加到您的控制台并按Enter键,您将看到解析的结果:

JSON.parse('{"name": "ABC Elementary", "addr": "3000 County Road 29 Alberta AL 36720-2817", "county": "Wilcox", "district": "6"}');

For more information, you can refer to the documentation that might be able to explain it better.

有关更多信息,请参阅可能能够更好地解释它的文档。

#2


0  

{
  "name": ABC Elementary, 
  "addr": 3000 County Road 29 Alberta AL 36720-2817, 
  "county": Wilcox, "district": 6; /* the ; here must delete */
}

if this is your json string. it is wrong in format(see comment above)

如果这是你的json字符串。格式错误(见上面的评论)

you can use alert(xhr.responseText) or console.log(xhr.responseText) to make sure you get the json

您可以使用alert(xhr.responseText)或console.log(xhr.responseText)来确保获得json

string you want.

你想要的字符串。

open your browser debug tool to check the console message,

打开浏览器调试工具以检查控制台消息,

if you are using old browsers do not support JSON.parse

如果您使用的是旧浏览器,则不支持JSON.parse

you can read here

你可以在这里阅读

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FJSON#Browser_compatibility

and

https://github.com/douglascrockford/JSON-js

#1


0  

From my understanding, JSON.parse() takes a string value, a stringified object and parses it.

根据我的理解,JSON.parse()接受一个字符串值,一个字符串化的对象并解析它。

For JSON.parse() to work for your case, we want the object returned to be:

要使JSON.parse()适用于您的案例,我们希望返回的对象为:

{"name": "ABC Elementary", "addr": "3000 County Road 29 Alberta AL 36720-2817", "county": "Wilcox", "district": "6"}

...BUT we want that wrapped in a string as the input for parse.

...但我们希望将包裹在字符串中的内容作为解析的输入。

Add this line to your console and press enter and you will see the result of the parse:

将此行添加到您的控制台并按Enter键,您将看到解析的结果:

JSON.parse('{"name": "ABC Elementary", "addr": "3000 County Road 29 Alberta AL 36720-2817", "county": "Wilcox", "district": "6"}');

For more information, you can refer to the documentation that might be able to explain it better.

有关更多信息,请参阅可能能够更好地解释它的文档。

#2


0  

{
  "name": ABC Elementary, 
  "addr": 3000 County Road 29 Alberta AL 36720-2817, 
  "county": Wilcox, "district": 6; /* the ; here must delete */
}

if this is your json string. it is wrong in format(see comment above)

如果这是你的json字符串。格式错误(见上面的评论)

you can use alert(xhr.responseText) or console.log(xhr.responseText) to make sure you get the json

您可以使用alert(xhr.responseText)或console.log(xhr.responseText)来确保获得json

string you want.

你想要的字符串。

open your browser debug tool to check the console message,

打开浏览器调试工具以检查控制台消息,

if you are using old browsers do not support JSON.parse

如果您使用的是旧浏览器,则不支持JSON.parse

you can read here

你可以在这里阅读

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FJSON#Browser_compatibility

and

https://github.com/douglascrockford/JSON-js