JSON strings and objects are converted to each other-JAVASCRIPT/JQUERY__JS

Source: Internet
Author: User
Preface

We know that if the conversion of JSON objects to Java objects is simple in Java, it is easy to implement using Alibaba's Fastjson or Jackjson , or use the JSON package in the net package in the tool can also be implemented, here is no longer to say, we talk about in JS objects and JSON strings between the conversion, and sometimes not only to deal with the business needs such a transformation, and you flexible use, debugging the front desk is easy, For example, a page can not track the JS process of this page, alert is the object, output [object], and so on, is not know what they want to know the JS object stored in the data, At this point, use the 2 methods I've described below to convert to a JSON string and then alert to know the properties and values of the object (though Console.log () and write directly to debugger; Can play the same debugging role) don't underestimate that, When you are unable to debug objects at the end of a particular project, this is the "Help" method to share my experience with everyone. OK, let's take a look at the following two conversion methods. method One: json.js to convert JSON and object to each other

To handle JSON data conveniently, JSON provides a json.js package, json.js free download address.

JSON converted to object

JSON converts to object
var obj = eval (' + str + ') ');
or 
var obj = Str.parsejson ();//json string converted to object
or
var obj = json.parse (str);//json string to Object


Note: If obj is a JSON object, then using the eval () function to convert (even multiple conversions) is a JSON object, but using the Parsejson () function can be problematic (throwing a syntax exception).

object into JSON

Object into JSON
you can use toJSONString () or json.stringify () to convert a JSON object to a JSON string.
var str=obj.tojsonstring ();//Convert object to JSON
or
var last=json.stringify (obj);//Convert object to JSON


above , in addition to the eval () function is JS from the band, the other points are from the Json.js package. The new version of JSON modifies the API by injecting the json.stringify () and Json.parse () two functions into the Javascript built-in object, which becomes the object.tojsonstring (), which becomes the Strin G.parsejson (). If the hint does not find tojsonstring () and Parsejson (), your JSON package version is too low.

method Two: Jquery.json to convert JSON and object to each other

Jquery.json is a plug-in to jQuery that enables easy conversion between objects and JSON strings. You can serialize JavaScript objects, numeric values, strings, and arrays to JSON strings, while converting JSON strings to JavaScript for free downloads of addresses.

object into JSON

Object
to json var thing = {plugin: ' Jquery-json ', Version:2.3};//js object
var str = $.tojson (thing);//Convert to JSON, result: ' {' Plugin ":" Jquery-json "," Version ": 2.3} '


JSON is turned into an object

JSON is converted to object
var  obj= $.evaljson (str);
Var Name=obj.plugin;//js object. Property, Result: "Jquery-json"
var version =obj.version;//results: 2.3


The above is a summary of the conversion between objects and JSON strings in Javasript.

reprint Please specify-author: java My life (Chen Leixing) Source: http://blog.csdn.net/chenleixing/article/details/45331003 Finally, seriously read the netizens, the great God, if there is a feeling that I this program ape where the wrong or wrong or you have a good proposal or suggestion or idea method, also hope You kindness give n seconds time to leave your valuable text (message), so that you, I, and the vast number of programs apes faster growth and Progress ....

Related Article

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.