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

So... I am trying to use JavaScript to parse an object returned from an PHP file. 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:

{
  "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.

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.

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.

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.

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

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

{"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:

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. 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

string you want.

open your browser debug tool to check the console message,

if you are using old browsers do not support 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