jquery JSON的解析方式

这里考虑都考虑的是服务器返回的是JSON形式的字符串的形式,对于利用JSONObject等插件封装的JSON对象,与此亦是大同小异,这里不再做说明。

这里首先给出JSON字符串集,字符串集如下:
复制代码 代码如下:

var data="
{
root:
[
{name:'1',value:'0'},
{name:'6101',value:'西安市'},
{name:'6102',value:'铜川市'},
{name:'6103',value:'宝鸡市'},
{name:'6104',value:'咸阳市'},
{name:'6105',value:'渭南市'},
{name:'6106',value:'延安市'},
{name:'6107',value:'汉中市'},
{name:'6108',value:'榆林市'},
{name:'6109',value:'安康市'},
{name:'6110',value:'商洛市'}
]
}";

这里以jquery异步获取的数据类型——json对象和字符串为依据,分别介绍两种方式获取到的结果处理方式。

1.对于服务器返回的JSON字符串,如果jquery异步请求没做类型说明,或者以字符串方式接受,那么需要做一次对象化处理,方式不是太麻烦,就是将该字符串放于eval()中执行一次。这种方式也适合以普通javascipt方式获取json对象,以下举例说明:

var dataObj=eval("("+data+")");//转换为json对象
alert(dataObj.root.length);//输出root的子对象数量
$.each(dataObj.root,fucntion(idx,item){
if(idx==0){
return true;
}

//输出每个root子对象的名称和值
alert("name:"+item.name+",value:"+item.value);
})

注:对于一般的js生成json对象,只需要将$.each()方法替换为for语句即可,其他不变。

2.对于服务器返回的JSON字符串,如果jquery异步请求将type(一般为这个配置属性)设为“json”,或者利用$.getJSON()方法获得服务器返回,那么就不需要eval()方法了,因为这时候得到的结果已经是json对象了,只需直接调用该对象即可,这里以$.getJSON方法为例说明数据处理方法:

$.getJSON("http://gaoyusi.blog.163.com/",{param:"gaoyusi"},function(data){
//此处返回的data已经是json对象
//以下其他操作同第一种情况
$.each(data.root,function(idx,item){
if(idx==0){
return true;//同countinue,返回false同break
}

alert("name:"+item.name+",value:"+item.value);

});
});

这里特别需要注意的是方式1中的eval()方法是动态执行其中字符串(可能是js脚本)的,这样很容易会造成系统的安全问题。所以可以采用一些规避了eval()的第三方客户端脚本库,比如JSON in JavaScript就提供了一个不超过3k的脚本库。

转载于:https://www.cnblogs.com/tcpcg/archive/2012/11/22/2782549.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274478.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

GitHub 搜索技巧:如何更有效地搜索 issue、repo 和更多信息

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 ruochuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

js 绘画js 绘画路径_绘画是一种技能,而不是才能

js 绘画js 绘画路径重点 (Top highlight)November 2019… some decided to start rocking a moustache, others were obsessed with baby Yoda. I decided to commit to drawing something every single day. As a way to keep myself accountable I started posting daily on …

tslib1.4安装小记

tslib,是触摸屏校准需要的库文件。1、tslib的安装需要执行以下命令sudo apt-get install autoconfsudo apt-get install automakesudo apt-get install libtool如果再非UBUNTU的机子上这参考:http://blog.chinaunix.net/uid-22391661-id-1774370.html 2运…

点击页面元素跳转IDE对应代码,试试这几个工具!

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 ruochuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

shields 徽标_徽标不够用时如何设计应用程序图标

shields 徽标What’s the first thing that comes to mind when you think about a particular app? Chances are, it’s the icon. And it’s certainly the first thing a user notices when deciding what app to install — similar to studying the supermarket shelves.…

文本光标,高亮选中一些出来

document.body.οnmοuseupfunction(){if(window.getSelection){if(window.getSelection().toString()!""){alert("显示弹出框");window.getSelection().removeAllRanges()//用于清除高亮选中}}else if(document.selection){if(document.selection.createR…

基于Sentry高效治理前端异常

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 ruochuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

zoom 用户被锁定_重新考虑Zoom的用户体验

zoom 用户被锁定Zoom is a household name now. It’s weird, but a new reality for 2020. I’ve been able to reliably stay in touch with so many people and even throw surprise birthday parties! It has been one of the products that has kept me busy through this…

或许是我们学错了方向?

大家好,我是若川。最近来了一些新朋友,感谢大家关注~相比于原生 JS 等源码。我们或许更应该学习正在使用的组件库的源码(如:element、antd、vant、semi、arco、tdesign 等),先从简单的看起,Butt…

Android 默认Tab标签大小及间距修改

一般来说,我都是用Android默认的Tab,但此时Android会根据你增加的Tab页面平均分配Tab标签,假如你只有两个Tab,那么长度将会很长,并且其高度略微过高,并不好看,网上解决这个问题有些是自定义Tab。…

ui设计看的书_5本关于UI设计的书

ui设计看的书Want to develop a better eye and vocabulary for judging layouts, type choices and imagery?是否想开发一种更好的眼神和词汇来判断布局,类型选择和图像? According to performance experts, the best way to learn is to gain lots o…

GitHub 这8大超实用小技巧,99.9%的人都不知道!

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 ruochuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

android的消息处理机制(图+源码分析)——Looper,Handler,Message

http://www.cnblogs.com/codingmyworld/archive/2011/09/12/2174255.html 还有相关的如下链接需要仔细check: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/ http://www.cnblogs.com/keyindex/articles/1822463.html转载于:https:…

案例研究设计与方法-罗伯_旭进口重新设计-用户体验案例研究

案例研究设计与方法-罗伯Asahi Imports is a Japanese grocery store located in central Austin, Texas. It has a passionate following, over fifty years’ history, and strong business growth. But its website is showing its age, and not hitting its full potential…

Taro v3.6 代号为「Reach」,已发布 canary 版本

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 lxchuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

axure rp 创建弹框_如何在Axure RP 9中创建交换机

axure rp 创建弹框Axure is a well-known prototyping tool with a lot of history, it has been around for years. You can create almost any kind of prototypes from simple ones to more complex ones with variables and logic without writing a single line of code (…

linux下使用cmake构建C/C++项目

cmake下载安装: 下载地址: http://www.cmake.org/cmake/resources/software.html wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz tar zxvf cmake-2.8.10.2.tar.gz cd cmake-2.8.10.2 ./configure gmake (不知道是不是必须的,提示要这么干…

用 Vue3 手撕了个企业级项目,真香!

最近几年,随着短视频、小程序、直播带货的火爆,前端开发工程师越来越热门,薪资待遇也快接近后端开发工程师了,前端领域进化为内卷重灾区。然而伴随着 Vue 3.0 的发布,前端技术也迎来了一次大革新,像是字节跳…

界面设计语言_使用任何语言设计界面的提示

界面设计语言Designing for international audiences is challenging. I spent most of my career in Australia designing exclusively in English. Australian English is ‘unique’ in the sense that we are really into slang, puns, idioms.为国际观众设计是具有挑战性的…

托管代码和非托管代码

托管代码和非托管代码有什么区别呢?从字面上理解,只是一个是被托管的,另一个是没有被托管的。但是,被托管的托管代码是被谁管着呢?让我们先来看看它们的定义。 托管代码:由公共语言运行库环境(而…