Example of conversion between JSON object and JSON string in javascript

1. Conversion methods supported by the jQuery plugin

code show as below:

$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象

Note: In jQuery 3.0 and later versions, this method has been deleted, please use the native method JSON.parse(str)

2. Conversion methods supported by browsers

(Firefox, chrome, opera, safari, ie9, ie8) and other browsers: 

code show as below:

JSON.parse(jsonstr); //可以将json字符串转换成json对象 
JSON.stringify(jsonobj); //可以将json对象转换成json对符串 

Note: ie8 (compatibility mode), ie7 and ie6 do not have JSON objects. It is recommended to use the official method of JSON and import json.js. 

3. Conversion methods supported by Javascript

eval('(' + jsonstr + ')'); //可以将json字符串转换成json对象,注意需要在json字符外包裹一对小括号

Note: ie8 (compatibility mode), ie7 and ie6 can also use eval() to convert a string to a JSON object, but these methods are not recommended. This method is not safe. eval will execute expressions in the json string. 
 

4. The official conversion method of JSON

http://www.json.org/ provides a json.js so that ie8 (compatibility mode), ie7 and ie6 can support JSON objects and their stringify() and parse() methods; 

This js can be obtained at https://github.com/douglascrockford/JSON-js, and json2.js is generally used now.

In the process of development, if you want to pass a small number of parameters to the front and back, you can directly use the ajax data function, pass it in json format, and the background Request is enough, but sometimes, you need to pass multiple parameters, so the background

It is very troublesome to accept multiple Requests. At this time, it must be passed in the form of a class or a collection.

For example: the frontend passes the JSON object in the format of the class:

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

If the spelled jsonUserInfo has no escape symbols, you need to convert it with var jsonArrayFinal = JSON.stringify(jsonArray); before passing it on.

code show as below:

$.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 multiple JSON arrays in class format, that is, the collection type:

For example:

[{"name":"a"},{"name","b"},{"name","c"}], it cannot be passed, and JSON.stringify must be used to convert the array object into a string , and then AJAX delivery.

For example, I have two variables, and I want to convert a to a string and b to a JSON object:

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

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

JSON.stringify(obj) converts JSON to a string. JSON.parse(string) converts the string to 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, but http://www.json.org/js.html provides a json.js, so that ie8 (compatibility mode), ie7 and ie6 can support JSON objects and Its stringify() and parse() methods; you can get this js at https://github.com/douglascrockford/JSON-js, generally use json2.js now.

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

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

There is also a method jQuery.parseJSON( json ) in jQuery to convert a string to JSON format, which accepts a JSON string in a standard format 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 to a string.

Guess you like

Origin blog.csdn.net/cdming/article/details/130212839