关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法
2019/6/27 22:18:00
本文主要是介绍关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. JSON.parse(jsonString): 在一个字符串中解析出JSON对象
var str = '[{"href":"baidu.com","text":"test","orgId":123,"dataType":"curry","activeClass":"haha"}]'; JSON.parse(str);
结果:
2. JSON.stringify(obj) : 将一个JSON对象转换成字符串
var obj = [{"href":"baidu.com","text":"test","orgId":123,"dataType":"curry","activeClass":"haha"}]; JSON.stringify(obj);
结果:
"[{"href":"baidu.com","text":"test","orgId":123,"dataType":"curry","activeClass":"haha"}]"
3. jQuery.parseJSON(jsonString) : 将格式完好的JSON字符串转为与之对应的JavaScript对象
var str = '[{"href":"baidu.com","text":"test","orgId":123,"dataType":"curry","activeClass":"haha"}]'; jQuery.parseJSON(str);
结果:
4.JSON.parse()和jQuery.parseJSON()的区别:
有的浏览器不支持JSON.parse()方法,使用jQuery.parseJSON()方法时,在浏览器支持时会返回执行JSON.parse()方法的结果,否则会返回类似执行eval()方法的结果,以上结论参考jquery 1.9.1 得出:
parseJSON: function( data ) { // Attempt to parse using the native JSON parser first if ( window.JSON && window.JSON.parse ) { return window.JSON.parse( data ); } if ( data === null ) { return data; } if ( typeof data === "string" ) { // Make sure leading/trailing whitespace is removed (IE can't handle it) data = jQuery.trim( data ); if ( data ) { // Make sure the incoming data is actual JSON // Logic borrowed from http://json.org/json2.js if ( rvalidchars.test( data.replace( rvalidescape, "@" ) .replace( rvalidtokens, "]" ) .replace( rvalidbraces, "")) ) { return ( new Function( "return " + data ) )(); } } } jQuery.error( "Invalid JSON: " + data ); },
以上这篇关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist