Resolves the conversion between JSON objects and strings

Source: Internet
Author: User
Tags eval json string to json

  This article is mainly about the JSON object and the conversion between the strings of the detailed introduction, the need for friends can come to the reference, I hope to help you.

In the process of development, if for a small number of parameters before and after the transmission, you can directly use the data function of Ajax, in JSON format delivery, background request can, but sometimes, you need to pass multiple parameters, so backstage   accept the request of a number of very troublesome, This is passed in the form of a class or a collection.     For example: The foreground passes the JSON object in the format of the class:   var jsonuserinfo = "{" Tusername ":" "+ UserName +" "," tinterest ":" + Interest + " "," Tsex ":" "+ Sex +" "," tcity ":" "+ City +" "," Tdetail ":" "+ Detail +" "}";   such as jsonuserinfo without escape symbol, var jsonarrayfinal = json.stringify (Jsonarray) is required, and then transferred. Code as follows: $.ajax (                    {          & nbsp             type: "Post",                   &NB Sp     URL: "receivehandler1.ashx"                       &N Bsp Data: {userinfo:jsonuserinfo, flag: "123456", Key: "654321"},                 & nbsp       DataType: ' text ',                         success:function (data) {            &NBSP ;               $ ("#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 it cannot be delivered. You must use Json.stringify to convert the array object to a string, and then do Ajax delivery.   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": "};  var b= ' {" name ":" Mike "," Sex ":" Female "," Age ":" The "} ';    in FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8 and other advanced browsers can directly use the JSON object Stringify () and Parse () Method.   Json.stringify (obj) converts JSON to a string. Json.parse (String) converts a string to JSON format;   The conversion above can be written as follows: Var a={"name": "Tom", "Sex": "Male", "Age": "};  var b= ' {" Name ":" Mike, "Sex": "Female", "Age": "; "} ' 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 provides a json.js, In this way IE8 (compatibility mode), IE7 and IE6 can support JSON objects and their stringify () and Parse () methods; You can get this JS on Https:// , usually now with 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": "I"} ';  var Ctoobj=eval ("(" +c+) ");  alert (typeof (Ctoobj));    jquery also has the method of converting a string to JSON format Jquery.parsejson (JSON), 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.    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.