Considerations for turning a JS array into a JSON string

Source: Internet
Author: User

In JS, an array is often converted to a JSON string to be sent to the backend.

This time, when defining an array data structure, it is important to note that there is a difference between a collection and an object in the JSON.

The definition of a collection is []; the definition of an object is {}.

At this point, the array needs to be created in a different way. For example, create a collection array:

varBizhi_info_modify_arr = []; 

Then create an array of objects:

Bizhi_info_modify_arr[i] = {};
bizhi_info_modify_arr[i]["id"] =$ ("#bizhi_list_id" +i). Val ();
bizhi_info_modify_arr[i]["Bizhi_list_provider"] =$ ("#bizhi_list_provider" +i). Val ();
bizhi_info_modify_arr[i]["Bizhi_list_catalog"] = $ ("#bizhi_list_catalog" +i). Val ();

The difference is that the collection array is just a collection, the key is no value, and the object array key has a value.

This makes it very simple to generate a JSON string, just call the system function json.stringify.

jsonstr = json.stringify (Bizhi_info_modify_arr);

Considerations for turning a JS array into a JSON string

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.