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 the spelled Jsonuserinfo no escape symbol, var jsonarrayfinal = json.stringify (Jsonarr ay); transfer after conversion

[HTML]View PlainCopy print?
  1. $.ajax (
  2. {
  3. Type: "Post",
  4. URL: "Receivehandler1.ashx",
  5. Data: {userinfo:jsonuserinfo, flag: "123456", Key: "654321"},
  6. DataType: "JSON",
  7. Success:function (data) {
  8. $ ("#divShow"). HTML (data);
  9. }
  10. });


This is a simple pass, if you pass multiple arrays, 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"}], you cannot pass, you must use Json.stringify to convert the array object to a string, then Ajax pass.

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

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

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:

[HTML]View PlainCopyprint?
    1. var a={"name": "Tom", "Sex": "Male", "Age": "24"};
    2. var b=' {' name ': ' Mike ', ' sex ': ' Female ', ' age ': ' 29 '} ';
    3. var atostr=json.stringify (a);
    4. var btoobj=Json.parse (b);
    5. Alert (typeof (Atostr)); String
    6. Alert (typeof (Btoobj));//object
    7. 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 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:

1>jquery plug-in supported conversion mode:

$.parsejson (JSONSTR); Jquery.parsejson (JSONSTR), you can convert a JSON string into 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, noting 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.

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.