Home >Web Front-end >JS Tutorial >Detailed explanation of conversion examples between JSON objects and strings
During the development process, if a small number of parameters are transferred to the front and back, you can directly use the ajax data function, 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 accept multiple Requests. At this time, they must be passed in the form of a class or a collection.
For example: the front desk 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 and then passed .
$.ajax( { type: "post", url: "ReceiveHandler1.ashx", data: { userInfo: jsonUserInfo, flag: "123456", key: "654321" }, dataType: "text", success: function(data) { $("#pShow").html(data); } });
If the front end passes multiple JSON arrays in class format, that is, the collection type:
For example:
[{"name":"a"},{"name","b"},{"name","c"}]
, it cannot be passed and must be used at this time JSON.stringify converts the array object into a string and then passes 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":"男","age":"24"}; var b='{"name":"Mike","sex":"女","age":"29"}';
In Firefox, chrome, opera, safari, ie9, Advanced browsers such as ie8 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":"男","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, However, 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 parse() methods; you can download them at github.com/ This js was obtained from douglascrockford/JSON-js. Generally, json2.js is used now.
ie8 (compatibility mode), ie7 and ie6 can use eval() to convert a string into a JSON object.
var c='{"name":"Mike","sex":"女","age":"29"}'; var cToObj=eval("("+c+")"); alert(typeof(cToObj));
jQuery also has a method for converting a string into JSON format jQuery.parseJSON (json), 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.
The above is the detailed content of Detailed explanation of conversion examples between JSON objects and strings. For more information, please follow other related articles on the PHP Chinese website!