How does ajax transfer arrays, JSON. parse, and JSON. stringify,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.