Home > Web Front-end > JS Tutorial > body text

Parse and convert between JSON objects and strings_jquery

WBOY
Release: 2016-05-16 17:08:36
Original
831 people have browsed it

During the development process, if you want to transfer a small number of parameters to the front and backend, you can directly use the data function of ajax, pass it in json format, and use the background Request. However, sometimes, you need to pass multiple parameters, so the background

It is troublesome to request multiple requests when accepting. At this time, it must be passed in the form of a class or a collection.


For example: the frontend passes JSON objects in class format:

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

If the jsonUserInfo is spelled out without escape symbols, var jsonArrayFinal = JSON.stringify(jsonArray); needs to be converted before passing.

Copy code The code is as follows:

$.ajax(
type : "post",
url: "ReceiveHandler1.ashx",
data: { userInfo: jsonUserInfo, flag: "123456", key: "654321" },
DataType: "text",
                                                                                                                                                                                                                                   . 🎜>

If the frontend passes multiple class formats JSON array, that is, collection type:

For example:
[{"name":"a"},{"name","b"},{"name","c"}], it cannot be passed. In this case, JSON.stringify must be used to convert the array object into a string and then pass it through AJAX.
For example, I have two variables. I want to convert a into a string and b into a JSON object:

var a={"name":"tom","sex":"male","age":"24"};

var b='{"name":"Mike","sex ":"Female","age":"29"}';

In advanced browsers such as Firefox, chrome, opera, safari, ie9, ie8, etc., you can directly use the stringify() and parse() methods of the JSON object.

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

The above conversion can be written as follows:

var a={"name":"tom","sex":"male","age":"24"};

var b='{ "name":"Mike","sex":"女","age":"29"}';

var aToStr=JSON.stringify(a);

var bToObj=JSON.parse(b );

alert(typeof(aToStr)); //string
alert(typeof(bToObj)); //object

JSON.stringify()

ie8 (compatibility mode), ie7 and ie6 do not have JSON objects, but
http://www.json.org/js.html

provides a json.js, so ie8 (compatibility mode), IE7 and IE6 can support JSON objects and their stringify() and parse() methods; you can get this js at https://github.com/douglascrockford/JSON-js, generally using json2.js now.

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

var c='{"name":"Mike","sex":"女","age":"29"}'; var cToObj=eval("(" c ")") ;

alert(typeof(cToObj));

jQuery also has a method for converting strings to JSON format, jQuery.parseJSON(json), which accepts a standard format JSON string and returns a parsed JavaScript (JSON) object. Of course, if you are interested, you can encapsulate a jQuery extension yourself. jQuery.stringifyJSON(obj) converts JSON into a string.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template