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

Detailed explanation of conversion examples between JSON objects and strings

零下一度
Release: 2018-05-18 11:27:51
Original
2012 people have browsed it

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 + "\"}";
Copy after login

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);                       
 } });
Copy after login

If the front end passes multiple JSON arrays in class format, that is, the collection type:

For example:

[{"name":"a"},{"name","b"},{"name","c"}]
Copy after login

, 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"}';
Copy after login

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()
Copy after login

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));
Copy after login

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!

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