js数组使用JSON.stringify和toString的区别 js数组使用JSON.stringify和toString的...

js\u6570\u7ec4\u4f7f\u7528JSON.stringify\u548ctoString\u7684\u533a\u522b

js\u4e2deval()\u548c$.parseJSON()\u7684\u533a\u522b\u4ee5\u53caJSON.stringify()1.\u7b2c\u4e00\u4e2a\u533a\u522b\u662f\uff1a\u5b89\u5168\u6027json\u683c\u5f0f\u975e\u5e38\u53d7\u6b22\u8fce\uff0c\u800c\u89e3\u6790json\u7684\u65b9\u5f0f\u901a\u5e38\u7528JSON.parse()\u4f46\u662feval()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u89e3\u6790\uff0c\u8fd9\u4e24\u8005\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1fJSON.parse()\u4e4b\u53ef\u4ee5\u89e3\u6790json\u683c\u5f0f\u7684\u6570\u636e\uff0c\u5e76\u4e14\u4f1a\u5bf9\u8981\u89e3\u6790\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u683c\u5f0f\u68c0\u67e5\uff0c\u5982\u679c\u683c\u5f0f\u4e0d\u6b63\u786e\u5219\u4e0d\u8fdb\u884c\u89e3\u6790\uff0c\u800ceval()\u5219\u53ef\u4ee5\u89e3\u6790\u4efb\u4f55\u5b57\u7b26\u4e32\uff0ceval\u662f\u4e0d\u5b89\u5168\u7684\u5982\uff1a[javascript]viewplaincopyvarstr='alert(1000.toString())';eval(str);JSON.parse(str);\u7528eval\u53ef\u4ee5\u89e3\u6790\uff0c\u5e76\u4e14\u4f1a\u5f39\u51fa\u5bf9\u8bdd\u6846\uff0c\u800c\u7528JSON.parse()\u5219\u89e3\u6790\u4e0d\u4e86\u3002\u5176\u5b9ealert\u5e76\u6ca1\u6709\u4ec0\u4e48\u574f\u5904\uff0c\u53ef\u6015\u7684\u662f\u5982\u679c\u7528\u6076\u610f\u7528\u6237\u5728json\u5b57\u7b26\u4e32\u4e2d\u6ce8\u5165\u4e86\u5411\u9875\u9762\u63d2\u5165\u6728\u9a6c\u94fe\u63a5\u7684\u811a\u672c\uff0c\u7528eval\u4e5f\u662f\u53ef\u4ee5\u64cd\u4f5c\u7684\uff0c\u800c\u7528JSON.parse()\u5219\u4e0d\u5fc5\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u3002\u6ce8\u610f\uff1a\u67d0\u4e9b\u4f4e\u7ea7\u7684\u6d4f\u89c8\u5668\u5c1a\u4e0d\u652f\u6301JSON.parse()\u300a\u9ad8\u6027\u80fdJavaScript\u300b\u4e00\u4e66\u5373\u6307\u51fa\uff1a\u8b66\u544a\uff1a\u5173\u4e8eJSON\u548ceval\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528eval\u662f\u5f88\u5371\u9669\u7684\uff0c\u7279\u522b\u662f\u7528\u5b83\u6267\u884c\u7b2c\u4e09\u65b9\u7684JSON\u6570\u636e\uff08\u5176\u4e2d\u53ef\u80fd\u5305\u542b\u6076\u610f\u4ee3\u7801\uff09\u65f6\uff0c\u5c3d\u53ef\u80fd\u4f7f\u7528JSON.parse()\u65b9\u6cd5\u89e3\u6790\u5b57\u7b26\u4e32\u672c\u8eab\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u6355\u6349JSON\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\uff0c\u5e76\u5141\u8bb8\u4f60\u4f20\u5165\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u8fc7\u6ee4\u6216\u8f6c\u6362\u89e3\u6790\u7ed3\u679c\u3002\u5982\u679c\u6b64\u65b9\u6cd5\u4ee5\u5907Firfox3.5\u3001IE8\u53caSafari4\u539f\u751f\u652f\u6301\u3002\u5927\u591a\u6570javascript\u7c7b\u5e93\u5305\u542b\u7684JSON\u89e3\u6790\u4ee3\u7801\u4f1a\u76f4\u63a5\u8c03\u7528\u539f\u751f\u7248\u672c\uff0c\u5982\u679c\u6ca1\u6709\u539f\u751f\u652f\u6301\u7684\u8bdd\uff0c\u4f1a\u8c03\u7528\u4e00\u4e2a\u7565\u5fae\u4e0d\u90a3\u4e48\u5f3a\u5927\u7684\u975e\u539f\u751f\u7248\u672c\u6765\u5904\u7406\u30022.\u7b2c\u4e8c\u4e2a\u533a\u522b\uff1aJSON.parse()\u89e3\u6790\u7684\u5fc5\u987b\u662fjson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8981\u4e0d\u62a5\u9519\uff0c\u800ceval()\u5219\u6ca1\u6709\u8fd9\u4e48\u4e25\u683c\u5728\u8fd9\u91cc\u201cjson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u201d\u662f\u6307\u8981\u6c42\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u5fc5\u987b\u7b26\u5408\u4e25\u683c\u7684JSON\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a\u5c5e\u6027\u540d\u79f0\u5fc5\u987b\u52a0\u53cc\u5f15\u53f7\u3001\u5b57\u7b26\u4e32\u503c\u4e5f\u5fc5\u987b\u7528\u53cc\u5f15\u53f7\u3002\u5982\u679c\u4f20\u5165\u4e00\u4e2a\u683c\u5f0f\u4e0d"\u5b8c\u597d"\u7684JSON\u5b57\u7b26\u4e32\u5c06\u629b\u51fa\u4e00\u4e2aJS\u5f02\u5e38json\u7684\u89e3\u6790\u65b9\u6cd5\u5171\u6709\u4e24\u79cd\uff1aeval\u548cJSON.parse()\uff0c\u5982\uff1a[javascript]viewplaincopyvarjsonStr='{"name":"lulu","sex":"female"}';varevalJson=eval('('+jsonStr+')');varjsonParseJson=JSON.parse(jsonStr);\u8fd9\u6837\u5c31\u628ajson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32jsonStr\u8f6c\u6362\u6210\u4e86JSON\u5bf9\u8c61\u3002\u4f46\u662f\u533a\u522b\u662f\uff1a[javascript]viewplaincopyvarage=27;varjsonStr='{"name":"lulu","sex":"female","age":++age}';varevalJson=eval('('+jsonStr+')');//\u4e0d\u62a5\u9519\u6b64\u65f6age\u7684\u503c\u662f28varjsonParseJson=JSON.parse(jsonStr);//\u62a5\u9519\u4ece\u4e0a\u9762eval()\u51fd\u6570\u7684\u7528\u6cd5\u6211\u4eec\u53ef\u4ee5\u770b\u51faeval()\u51fd\u6570\u5728\u89e3\u6790json\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u65f6\u8981\u52a0\u4e0a\u5706\u62ec\u53f7\u5982eval('('+jsonStr+')'),\u8fd9\u662f\u56e0\u4e3a\uff1aeval\u672c\u8eab\u7684\u95ee\u9898\u3002\u7531\u4e8ejson\u662f\u4ee5\u201d{}\u201d\u7684\u65b9\u5f0f\u6765\u5f00\u59cb\u4ee5\u53ca\u7ed3\u675f\u7684\uff0c\u5728JS\u4e2d\uff0c\u5b83\u4f1a\u88ab\u5f53\u6210\u4e00\u4e2a\u8bed\u53e5\u5757\u6765\u5904\u7406\uff0c\u6240\u4ee5\u5fc5\u987b\u5f3a\u5236\u6027\u7684\u5c06\u5b83\u8f6c\u6362\u6210\u4e00\u79cd\u8868\u8fbe\u5f0f\u3002\u52a0\u4e0a\u5706\u62ec\u53f7\u7684\u76ee\u7684\u662f\u8feb\u4f7feval\u51fd\u6570\u5728\u5904\u7406JavaScript\u4ee3\u7801\u7684\u65f6\u5019\u5f3a\u5236\u5c06\u62ec\u53f7\u5185\u7684\u8868\u8fbe\u5f0f\uff08expression\uff09\u8f6c\u5316\u4e3a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4f5c\u4e3a\u8bed\u53e5\uff08statement\uff09\u6765\u6267\u884c\u3002\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4f8b\u5982\u5bf9\u8c61\u5b57\u9762\u91cf{}\uff0c\u5982\u82e5\u4e0d\u52a0\u5916\u5c42\u7684\u62ec\u53f7\uff0c\u90a3\u4e48eval\u4f1a\u5c06\u5927\u62ec\u53f7\u8bc6\u522b\u4e3aJavaScript\u4ee3\u7801\u5757\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u8bb0\uff0c\u90a3\u4e48{}\u5c06\u4f1a\u88ab\u8ba4\u4e3a\u662f\u6267\u884c\u4e86\u4e00\u53e5\u7a7a\u8bed\u53e5\u3002JSON.stringify()JSON.stringify()\u662f\u628ajson\u6570\u636e\u8f6c\u5316\u6210json\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5982\uff1a[javascript]viewplaincopyvarjsonObj={"name":"lulu","sex":"female"};varjsonStr=JSON.stringify(jsonObj);\u7ed3\u679c\u662f\uff1a'{"name":"lulu","sex":"female"}'

arr.toString()\u662f\u5c06\u6570\u7ec4\u8f6c\u5316\u6210\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u4e0d\u5e26 [ ]

\u800cJSON.stringify(arr)\u662f\u4ece\u4e00\u4e2a\u5bf9\u8c61\u89e3\u6790\u51faJSON\u5b57\u7b26\u4e32\uff0c\u662f\u5e26[]\u7684

js数组使用JSON.stringify和toString的区别如下:
1、JSON.stringify()是json对象的一个方法,它可以把对象序列化成json字符串。
toString是一个js对象的方法,任何对象都会继承这个方法,也就是Object.prototype.toString(),返回值类型为:[object Object]
2、用法上的区别举例:
比如定义:obj =[1,2,3]
那么用toString的结果是"1,2,3",用JSON.stringify()的结果是"[1,2,3]"

二者处理后的结果是不同的。

JSON.stringify
结果带 [ ]
toString
结果不带 [ ]

扩展阅读:json在线工具 ... json解密网站 ... 游戏json文件解密 ... js遍历json数组 ... 把string列表转化为json ... string转json对象数组 ... js 多个json遍历 ... 用json把string转为map ... 游戏json文件怎么打开 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网