Convert json to String and String to json and determine the object type

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

Function ajaxGetMenuList () {$. getJSON ("login. do ", function (json) {var r =" "; zNodes + =" ["; $ (json. menuList ). each (function (I) {r = json. menuList [I]; if (I> 0) zNodes + = ","; zNodes + = '{'; zNodes + = 'id: '+ r. SId; zNodes + = ', pId:' + r. SPid; zNodes + = ', name: "' + r. STitle; if (r. SPid = 0) zNodes + = '", open: true'; else zNodes + ='" '; zNodes + ='} ';}) zNodes + = "]"; zNodes = stringToJSON (zNodes); createTree (zNodes );}/*** Convert json to String * @ param {Object} obj * @ memberOf {TypeName} * @ return {TypeName} */function jsonToString (obj) {var THIS = this; switch (typeof (obj) {case 'string': return '"' + obj. replace (/(["\])/g, '\ $1') + '"'; case 'array': return '[' + obj. map (THIS. jsonToString ). join (',') + ']'; case 'object': if (obj instanceof Array) {var strArr = []; var len = obj. length; for (var I = 0; I <len; I ++) {strArr. push (THIS. jsonToString (obj [I]);} return '[' + strArr. join (',') + ']';} else if (obj = null) {return 'null';} else {var string = []; for (var property in obj) string. push (THIS. jsonToString (property) + ':' + THIS. jsonToString (obj [property]); return '{' + string. join (',') + '}';} case 'number': return obj; case false: return obj ;}} /*** convert String to json * @ param {Object} obj * @ return {T YpeName} */function stringToJSON (obj) {return eval ('+ obj +');} // judge the object type function getType (x) {& nbsp; & nbsp; if (x = null) {& nbsp; & nbsp; return "null"; & nbsp; & nbsp; & nbsp ;}& nbsp; & nbsp; & nbsp; var t = typeof x; & nbsp; & Nbsp; if (t! = "Object") {& nbsp; & nbsp; return t; & nbsp ;}& nbsp; & nbsp; & nbsp; var c = Object. prototype. toString. apply (x); & nbsp; c = c. substring (8, c. length-1); & nbsp; if (c! = "Object") {& nbsp; & nbsp; return c; & nbsp ;}& nbsp; & nbsp; & nbsp; if (x. constructor = Object) {& nbsp; & nbsp; return c & nbsp; & nbsp ;}& nbsp; & nbsp; & nbsp; if ("classname" in x. prototype. constructor & nbsp; & nbsp; & typeof x. prototype. constructor. classname = "string") {& nbsp; & nbsp; return x. constructor. prototype. classname; & nbsp ;}& nbsp; & nbsp; & nbsp; return "<unknown type>"; & nbsp; & nbsp ;}

 

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.