How does ajax transfer arrays, JSON. parse, and JSON. stringify,
[How does ajax send arrays to php? What are the solutions ?]
First understand:
Data has two parameter passing types: an Object, but a String ). The object type must beKey-value pairsFormat.
Based on data, only two types of data can be transferred (jq converts the array to a string). Therefore, you need some methods to convert the array to the json format, it is unrealistic to manually use this function. If this array is large enough, there must be a method that has been formed to implement this function.
After Baidu, I thought it was a reliable method.
1. $. toJson ("array to be converted") 2. JSON. stringify ("array to be converted ").
Now I save the value of the selected multiple selection box in an array checked_value [] and want to pass it to php. (Not an object or a string, it is a simple array)
Copy the array and convert itKey-Value Pair]Json format.
Because jsonstr is a normal array.
Therefore, when writing ajax data, you cannot directly write data: jsonstr, but data: {jr: jsonstr },
This is the key-value pair. The default key starts from 0 and the value is the value in the original array.
(It was not too clear at the time, and the results of various test modifications were obtained)
It's so cool, right?
I was confused when I wrote it in the space. Now I have re-checked the information and understood it.
Let's talk about the original usage of the two methods.
1. JSON. parse ("array to be converted ")
2. JSON. stringify ("the array to be converted ").
First, the usage of the two methods is different.
JSON. stringify is fromObjectParsingString.
JSON. parse is fromStringParsingJson(Key-value pairs ).
Therefore, if the parameters do not match, the conversion fails.
Usage:
JSON. parse: String-> key-Value Pair
Var str = '{"address": "mingshui", "phone": "1237828 "}';
JSON. parse (str );
>
Address: "mingshui"
Phone: 1237828"
Note that brackets are enclosed in single quotes. Double quotation marks are troublesome. It seems that \ is added before the double quotation marks in curly brackets. Otherwise, the syntax is incorrect.
JSON. stringify: Object-> string
Var a = {a: 1, B: 2}
JSON. stringify ()
>
"{" A ": 1," B ": 2 }"
Paste a very powerful parsing.
Http://www.jb51.net/article/29893.htm