JSON 获取属性值的方法

1. 对象名.属性名, 类似于高级语言的写法


var obj = '{"name":"wisdo", "age":"20"}';
var data = eval('('+ obj +')');
alert(obj.name);
2. 以数组索引的方式来访问


var obj = '{"name":"wisdo", "age":"20"}';
var data = eval('('+ obj +')');
alert(obj[0]);  // 输出的是 name
3. 以字典索引的方式来访问


var obj = '{"name":"wisdo", "age":"20"}';
var data = eval('('+ obj +')');
alert(obj['name']);  // 输出的是 name 值
 在通常的情况下,一般是使用第一种方法,即通过对象名.属性名 的方式来访问,这样对于使用高级语言的编程很直观,也很容易理解,但这也有局限性,属性的名称要规范,key 要以字母或下划线开头的包括字母、下划线和数字的字符串,不能以数字开头.


总结
  data.key和data[’key’]各自有自己的应用场景,一般情况使用data.key即可,也比较直观(它符合其它高级语言中访问对象中属性的方式);当key为一个变量时,并且使用在循环中,用data['key']这种方式。
for(var i=0; i < 10; i++) {
s += data['key' + i];  //循环调用,可简化代码}
  以数组索引的方式来访问,虽然不推荐,但也是有其应用价值的;如当建立一个与数据库中id一一对应的map对象的时候,可直接用id的数值做key,虽然你可以给它加上一个字母前缀来让它符合合法的变量名的标准并让它的数据能通过data.key的方式访问.

json获取变量属性值???

07-14

[code=Java]rnrn"data":["abstr":"MetadataTest-Dublin","baseUrl":"http:\/\/159.226.111.11:8080","id":"100101-11853"rn,"prmTitle":"MetadataTest-Dublin","producer":"MetadataTest-Dublin","searchEntry":null,"status":"0","tablename"rn:"md_data","title":"MetadataTest-Dublin","abstr":"homesong","baseUrl":"http:\/\/159.226.111.11:8080"rn,"id":"100101-11851","prmTitle":"test-dublin","producer":"homesong","searchEntry":null,"status":"2","tablename"rn:"md_data","title":"test-dublin"],"dataCount":2,"docs":["authorName":"jounal songj","commitor":"","id"rn:"30","prmTitle":"jounal","publicationDate":"2008-07-01T00:00:00","publicationName":"地球信息科学","publicationPress"rn:"","publicationYear":2008,"searchEntry":null,"status":"","submitTime":null,"tablename":"document","title"rn:"jounal","type":0,"authorName":"黄京鸿","commitor":"","id":"34","prmTitle":"高中地理教学开展因材施教的探讨 ","publicationDate"rn:"2008-07-01T00:00:00","publicationName":"西南师范大学学报(自然科学版)","publicationPress":"","publicationYear":2008rn,"searchEntry":null,"status":"","submitTime":null,"tablename":"document","title":"高中地理教学开展因材施教的探讨 ","type"rn:0,"authorName":"宋佳","commitor":"","id":"35","prmTitle":"语义网的产生与发展","publicationDate":"2009-07-01T00rn:00:00","publicationName":"语言网","publicationPress":"北京:科技出版社","publicationYear":2009,"searchEntry":nullrn,"status":"","submitTime":null,"tablename":"document","title":"语义网的产生与发展","type":1,"authorName":"dd"rn,"commitor":"","id":"32","prmTitle":"test jounal","publicationDate":"1970-01-01T00:00:00","publicationName"rn:"rrr","publicationPress":"","publicationYear":1970,"searchEntry":null,"status":"","submitTime":nullrn,"tablename":"document","title":"test jounal","type":0,"authorName":"jounal songj3","commitor":"","id"rn:"31","prmTitle":"jounal23","publicationDate":"2008-07-01T00:00:00","publicationName":"地球信息科学23","publicationPress"rn:"bb","publicationYear":2008,"searchEntry":null,"status":"","submitTime":null,"tablename":"document"rn,"title":"jounal23","type":1],"docsCount":5rn[/code]rnrnrn上面的json数据 我只想取到id的属性rnrnrn[code=Java]rnhttp://localhost:8080/Portal/manageMetadata/manageMetadata.jsp?id=100101-11854rn[/code]rn作为id的变量使用??rn 这个怎么获取

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭