Resolves the reciprocal conversion between JSON objects and strings _jquery

Source: Internet
Author: User

In the process of development, if for a small number of parameters before and after the transmission, you can directly use the AJAX data function, in JSON format delivery, background request can, but sometimes, need to pass multiple parameters, so backstage

Request a number of times to accept a lot of trouble, at this time to follow the class format or collection of the form of delivery.


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

var jsonuserinfo = "{\" tusername\ ": \" "+ UserName +" \ "," tinterest\ ": \" "+ Interest +" \ ", \" tsex\ ": \" "+ Sex +" \ ", \" Tcit Y\ ": \" "+ City +" \ ", \" tdetail\ ": \" "+ detail +" \ "}";

If no escape symbol is spelled out, the var jsonarrayfinal = json.stringify (Jsonarray) is required, and Jsonuserinfo is transferred.

Copy Code code as follows:

$.ajax (
{
Type: "Post",
URL: "Receivehandler1.ashx",
Data: {userinfo:jsonuserinfo, flag: "123456", Key: "654321"},
DataType: "Text",
Success:function (data) {
$ ("#divShow"). HTML (data);
}
});

If the foreground passes a JSON array of multiple class formats, that is, the collection type:

For example:

[{"Name": "A"},{"name", "B"},{"name", "C"}], then cannot be delivered, the array object must be converted to a string using Json.stringify, and then Ajax can be passed.

For example, I have two variables, I want to convert a to a string, 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,IE8 can directly use the stringify () and Parse () methods of the JSON object.

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

The above conversion can be written like this:
var a={"name": "Tom", "Sex": "Male", "Age": "24"};
var b= ' {' name ': ' Mike ', ' sex ': ' Female ', ' 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 () method; You can get this JS on the HTTPS://GITHUB.COM/DOUGLASCROCKFORD/JSON-JS, usually using json2.js now.

IE8 (compatible mode), IE7 and IE6 can use eval () to convert a string to a JSON object.

var c= ' {' name ': ' Mike ', ' sex ': ' Female ', ' age ': ' 29 '} ';
var ctoobj=eval ("(" +c+ ")");
Alert (typeof (Ctoobj));

jquery also has a method Jquery.parsejson (JSON) that converts a string to JSON format, accepts a well-formed JSON string, and returns the parsed JavaScript (JSON) object. Of course, if you are interested in encapsulating a jQuery extension yourself, Jquery.stringifyjson (obj) converts JSON to a 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.