1.eval () different from Json.parse
Eval ()
var // Global Variables var jsonstr1 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ++c} '; var json1 = eval (' (' + jsonstr1 + ') '); Console.log (json1.value);
C is a global variable of 1, but after Eval parsing, the global variable becomes 2! (This is a dangerous way of writing)
Json.parse
var // Global Variables var jsonstr1 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ++c} '; var json2 = json.parse (JSONSTR1); Console.log (json2.value);
IE under Error:
Google under the error:
and the Json.parse will error.
2. Add double quotation marks to ++c, because it becomes a string.
var // Global Variables var jsonstr1 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ' ++c '} '; var json2 = json.parse (JSONSTR1); Console.log (json2.value);
Results:
3. I think a lot of people are tangled in double quotes , and here we're going to focus on the test.
A:json.parse
varc = 1;//Global Variables varDate =NewDate (); Console.log (Date.tolocaledatestring ()); //var jsonstr1 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ' ++c '} ';//key, value has double quotation marks varJsonstr2 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ++c} ';//++c Morning double quotes only //var jsonstr3 = ' {name: ' A ', company: ' B ', value: ' ++c '} ';//Only the value has double quotes //var jsonstr4 = ' {name:a,company:b,value:++c} ';//all Matchless quotes //var json1 = Json.parse (JSONSTR1); varJson2 =Json.parse (JSONSTR2); //var json3 = Json.parse (JSONSTR3); //var json4 = Json.parse (JSONSTR4); //Console.log (json1);Console.log (Json2); //Console.log (json3); //Console.log (json4);
The results are in turn:
Visible Json.parse only the first standard form will be resolved correctly
B:eval ()
varc = 1;//Global Variables varDate =NewDate (); Console.log (Date.tolocaledatestring ()); varJsonstr1 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ' ++c '} ';//keys, values have double quotes //var jsonstr2 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ++c} ';//Only ++c morning double quotes //var jsonstr3 = ' {name: ' A ', company: ' B ', value: ' ++c '} ';//Only the value has double quotes //var jsonstr4 = ' {name:a,company:b,value:++c} ';//all Matchless quotes varJson1 = eval (' (' +jsonstr1+ ') '); //var json2 = eval (' (' +jsonstr2+ ') '); //var json3 = eval (' (' +jsonstr3+ ') '); //var json4 = eval (' (' +jsonstr4+ ') ');Console.log (Json1+" "+json1.value); //Console.log (json2+ "" +json2.value); //Console.log (Json3 + "" + json3.value); //Console.log (json4+ "" +json3.value);
The results are in turn:
In addition to the last of the eval, the others are parsed correctly.
4. There is one more question: Why do I have to add parentheses when eval () is parsed?
Let's see what happens without parentheses:
var // Global Variables var New Date (); Console.log (Date.tolocaledatestring ()); var jsonstr1 = ' {' name ': ' A ', ' company ': ' B ', ' Value ': ' ++c '} '; // keys, values have double quotes var json1 = eval (jsonstr1); Console.log (json1+ " " +json1.value);
Results:
There is nothing wrong with it here.
That's because eval () is equivalent to an execution environment, and when you're not in parentheses, JSONSTR1 is considered a compound statement. When you run it, you will parse the characters individually.
But when you add parentheses, jsonstr1 as an expression. It is recognized as an object from the beginning of the parentheses.
5. And then you think, why does this parenthesis add "()", Cannot add "{}"?
The answer is: Of course, can add "{}", but will be an error only ~. — —!
It's time to make it clear what the JSON format is!
{"Name": "A", "Company": "B"} This is the standard form.
Json.parse and Eval () for parsing JSON problems