简体   繁体   English

JSON.parse()如何做事?

[英]How does JSON.parse() do its thing?

So... I am trying to use JavaScript to parse an object returned from an PHP file. 所以...我正在尝试使用JavaScript解析从PHP文件返回的对象。 The relevant code looks like this: 相关代码如下:

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?") . 我从alert("Parsing")得到一个警报,但是没有从alert("Is this being skipped?")得到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() ,我也没有得到任何超出该点的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. 另外,我知道其他人也对此提出了疑问,而且我一直在寻找这些答案,但是似乎没人对知道JSON.parse()对数据所做的确切工作感兴趣。 If anyone could enlighten me, I would be very grateful. 如果有人能启发我,我将不胜感激。

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. 有关更多信息,您可以参考可能会更好地解释它的文档

{
  "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. 如果这是您的json字符串。 it is wrong in format(see comment above) 格式错误(请参阅上面的注释)

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 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON?redirectlocale=zh-CN&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FJSON#Browser_compatibility

and

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

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

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