As we all know, JSON is a kind of lightweight data interchange format, it is ideal for JS processing data!
Skilled written Xxx.json file and operation of small partners, I said that the problem is not a big problem, you can ignore this baby's article, but also hope that the big guy pointed ('? ') )
?
Problem one, data format issues
The first ①, the exact wording;
[{"Name": "AAA Chinese Test", "Payment": "999", "url": "Http://www.bejson.com", "asdf": "Asdf"}, {"Name": "BBB", "Payment": "888", "url": "Http://www.bejson.com", "asdf": "Asdf"}, {"Name": "CCC Chinese Test", "Payment": "777", "url": "Http://www.bejson.com", " ASDF ":" Asdf "}, {" Name ":" DDD "," Payment ":" 666 "," url ":" Http://www.bejson.com "," asdf ":" Asdf
]
Section ②, minefield error notation:
[{name: "AAA Chinese Test", Payment: "999", URL: "http://www.bejson.com", asdf: "Asdf"}, {name: "BBB", Payment: "888", URL: "/http/ Www.bejson.com ", asdf:" Asdf "}, {name:" CCC Chinese Test ", Payment:" 777 ", URL:" http://www.bejson.com ", asdf:" Asdf "}, {name:" ddd ", Payment:" 666 ", url:" http://www.bejson.com ", asdf:" ASDF "}]
See clearly ah everybody, there is no quotation mark key↑ This, you put in the Xxx.json file test is not pass, so will error of Wow, this is an extra-large misunderstanding!
If you put in the xxx.js so that no one cares about you, but you now in the Xxx.json to write this is absolutely impossible;
JSON official website The latest specifications have provisions Ah, as long as the string, whether it is a key or value need to enclose the quotation marks, do not underestimate the quotation marks, like this baby, write JS write habit without quotes, the result simply did not find this problem, wasted a good time/(ㄒoㄒ)/~~
Do not refute why the quotation marks, the official website is so defined.
?
Question two, data structure issues
JSON is a string of strings, except that elements are labeled with a particular symbol.
{} Double brackets represent objects
[] brackets represent the array
"" In double quotes is a property or value
In summary: {"key": "Value"}
Key defines the property name, value can be string, number, Boolean, array, object equivalence;
So you can use this structure to write JSON:
[{"Name": "Chuyue0"},{"name": "Chuyue1"}]//the second kind of separate writing, this baby is accustomed to use this??
I think of a problem, see the big guy solution, JS has a set of statements, if so, what is the form, the same array?
Question one or two is a small problem that should be paid attention to at ordinary times, as long as careful, skilled, the problem is not big!
?
problem three, mutual conversion problem
During data transfer, JSON is passed as a string, and JS is a JSON object, so you need to use a specific method to convert it!
var str = ' {' name ': ' chuyue0 ', ' sex ': ' Female '} ';//json string var data=eval ("(" +str+ ")"); /Convert to JSON object Console.log (data.name)
? parsing json with eval () is unsafe
Most modern browsers (ie9+) now have native JSON objects that provide method parsing!
① JSON strings into JSON objects
var str = ' {' name ': ' chuyue0 ', ' sex ': ' Female '} '; var data=json.parse (str); Console.log (data);
If the parse () method throws an exception, add a test of the JSON string (typeof (str) = = = ' String ')
How to use jquery: $.parsejson (str); or Jquery.parsejson (str);
After converting to an object, you can do other things. O (* ̄▽ ̄*) ブ
② JSON objects into JSON strings
var obj2 = {"Name": "Chuyue0", "Sex": "Female"}; // JSON Object var str2 = json.stringify (obj2); Console.log (str2);
? If data is already a JSON object, an eval () conversion will cause an error, such as:
Add
Array to string: Array.join (', ')
String to array: Str.split (', ')
?
The above is in the operation of the JSON encountered a variety of problems, I hope that through their own mistakes to help people ~~~~~~
The entire pen heart Oh ~ ~ ~
JSON operational Issues Summary