Parses the conversion between JSON objects and strings.

Source: Internet
Author: User
This article mainly introduces the conversion between JSON objects and strings in detail. For more information, see, I hope to help you. This article mainly introduces the conversion between JSON objects and strings in detail. If you need it, you can refer to it for help.

During the development process, if the front and back ends of a few parameters are passed, you can directly use the ajax data function, which is passed in json format, and the background Request can be used, but sometimes, multiple parameters need to be passed, so that the background

When receiving the Request, it is very troublesome. At this time, the Request must be passed in the form of a class or a set.


For example, the foreground transmits a JSON object in the class format:

Var jsonUserInfo = "{\" TUserName \ ": \" "+ userName +" \ ", \" TInterest \ ": \" "+ interest + "\", \ "TSex \": \ "" + sex + "\", \ "TCity \": \ "" + city + "\", \ "TDetail \": \ "" + detail + "\"}";

For example, if the spelled jsonUserInfo does not have an escape character, var jsonArrayFinal = JSON. stringify (jsonArray) is required.

The Code is as follows:


$. Ajax (
{
Type: "post ",
Url: "ReceiveHandler1.ashx ",
Data: {userInfo: jsonUserInfo, flag: "123456", key: "654321 "},
DataType: "text ",
Success: function (data ){
$ ("# PShow" ).html (data );
}
});


If the foreground transmits a JSON array of multiple class formats, that is, the set type:

For example:

[{"Name": "a" },{ "name", "B" },{ "name", "c"}], cannot be passed, JSON is required. stringify converts the array object to a string and then performs AJAX transmission.

For example, if I have two variables, I want to convert a to a string and convert B to a JSON object:

Var a = {"name": "tom", "sex": "male", "age": "24 "};
Var B = '{"name": "Mike", "sex": "female", "age": "29 "}';

Advanced browsers such as Firefox, chrome, opera, safari, ie9, and ie8 can directly use the stringify () and parse () Methods of JSON objects.

JSON. stringify (obj) converts JSON into a string. JSON. parse (string) converts a string to JSON format;

The preceding conversions can be written as follows:
Var a = {"name": "tom", "sex": "male", "age": "24 "};
Var B = '{"name": "Mike", "sex": "female", "age": "29 "}';
Var aToStr = JSON. stringify ();
Var bToObj = JSON. parse (B );
Alert (typeof (aToStr); // string
Alert (typeof (bToObj); // object

JSON. stringify ()

Ie8 (compatible mode), ie7 and ie6 do not have JSON objects, but ie7 and ie6 support JSON objects and their stringify () and parse () methods; you can obtain this js on #. Generally, json2.js is used.

Ie8 (compatible mode), ie7 and ie6 can use eval () to convert strings into JSON objects,

Var c = '{"name": "Mike", "sex": "female", "age": "29 "}';
Var cToObj = eval ("(" + c + ")");
Alert (typeof (cToObj ));

JQuery also provides the JSON format conversion method jQuery. parseJSON (json). It accepts a standard JSON string and returns the parsed JavaScript (JSON) object. If you are interested, you can encapsulate a jQuery extension. jQuery. stringifyJSON (obj) converts JSON into a string.

For more articles about how to parse the conversion between JSON objects and strings, refer to PHP!

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.