Several ways to convert foreground JSON objects into string conversions

Source: Internet
Author: User
Tags string to json

In recent work, the use of JSON data transfer, especially from the front to the background, the foreground can directly use the AJAX data function, in JSON format, the background request can be, but sometimes, need to pass a number of parameters, the background using request to receive. Sometimes pass a few values, fortunately receive. However, if a JSON array is passed, the request is cumbersome when the background accepts it, and is passed in the form of a class or a collection. For example, the following example:

The foreground passes the JSON object in the format of the class:

var jsonuserinfo = "{\" tusername\ ": \" "+ UserName +" \ ", \" tinterest\ ": \" "+ Interest +" \ ", \" tsex\ ": \" "+ Sex +" \ ", \" Tcit Y\ ": \" "+ City +" \ ", \" tdetail\ ": \" "+ detail +" \ "}"; If you spell the Jsonuserinfo no escape symbol, you need var jsonarrayfinal = json.stringify (Jsonarray);

$.ajax (                    {                        type: "Post",                        URL: "Receivehandler1.ashx",                        data: {userinfo:jsonuserinfo, flag: "123456", Key: "654321"},                        DataType: "JSON",                        success:function (data) {                            $ ("#divShow"). HTML (data);}                        );

It is simple to pass if multiple arrays are passed,The foreground passes a JSON array of multiple class formats, that is, the collection type. Background processing is cumbersome, such as:

[{" name ":" A "},{" name "," B "},{" name "," C "}], it cannot be passed, You must use Json.stringify to convert the array object to a string and then the Ajax pass.

For example: Two parameter variables, one is a string, and the other is an array.

var a={" name ":" Tom "," Sex ":" Male "," Age ":"};  ",
var b= ' {" name ":" Mike "," Sex ":" Female "," Age ":"} ";

Advanced browsers such as FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8 can directly use the stringify () and Parse () methods of JSON objects.

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

In the conversion to the general use of two ways, one is json.stringify (obj), converted to a string,Json.parse (String) to the string into JSON format, but one json.js support. However, in many browsers are now supported, can be used directly.

The above conversions 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));//objectjson.stringify ()

IE8 (compatibility mode), IE7 and IE6 do not have JSON objects, but http://www.json.org/js.html provides a json.js so that IE8 (compatibility mode), IE7 and IE6 can support JSON objects and their stringify () and the Parse () method; You can get this JS on Https://github.com/douglascrockford/JSON-js, which is usually used json2.js.

IE8 (compatibility 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 into JSON format, accepts a JSON string in a standard format, and returns the parsed JavaScript (JSON) object. Of course, if you are interested in encapsulating a jQuery extension yourself, Jquery.stringifyjson (obj) converts the JSON to a string.

This is only a method, there are several ways, you can look at:

how the 1>jquery plugin supports conversion:

$.parsejson (JSONSTR);//jquery.parsejson (JSONSTR), you can convert a JSON string to a JSON object
2> Browser-supported conversion mode(FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8) and other browsers:
Json.parse (JSONSTR); You can convert a JSON string into a JSON object
json.stringify (jsonobj);//You can convert a JSON object to a JSON string
Note: IE8 (compatibility mode), IE7 and IE6 do not have JSON objects, it is recommended to use the official JSON method, the introduction of Json.js.

3>javascript supported conversion modes:
eval (' (' + jsonstr + ') ');//You can convert a JSON string to a JSON object, note that you need to wrap a pair of parentheses outside the JSON character
Note: IE8 (compatibility mode), IE7 and IE6 can also use eval () to convert strings to JSON objects, but these methods are not recommended, which is unsafe for eval to execute an expression in a JSON string.

4>json Official way of conversion:
http://www.json.org/provides a json.js so that IE8 (compatibility mode), IE7 and IE6 can support JSON objects as well as their stringify () and Parse () methods;
can get this JS on Https://github.com/douglascrockford/JSON-js, generally now use json2.js.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Several ways to convert foreground JSON objects into string conversions

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.