【发布时间】:2012-10-25 10:02:23
【问题描述】:
我有这个 JSON 文件:http://danish-regional-data.googlecode.com/svn/trunk/danish_regional_data.json
如何删除所有邮政编码的所有属性within_5_km、within_10_km、within_25_km、within_50_km、within_100_km?
我读过这个问题:Remove a JSON attribute
$(document).ready(function() {
$.getJSON("post.json", function(data) {
var pc = data.postalcodes;
for (var id in pc) {
if(pc.hasOwnProperty(id)) {
for(var attr in pc[id]) {
if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
delete pc[id][attr];
}
}
}
}
$("#json").html(pc);
});
});
【问题讨论】:
-
嗯,您需要递归循环遍历您的 JSON 对象,搜索这些属性,并在找到它们时删除它们,根据您链接问题中的答案。
-
警告 12MB
.json问题中的文件 -
如果您希望在调用 JSON 后执行此操作以获取数据,那将是多余的,因为您必须先下载它们才能删除它们。
-
@Lübnah - 你能告诉我怎么做吗?因为在链接的问题中没有循环或搜索:/
-
@JaroslawWaliszko 好的,这有点迂腐,但从技术上讲是正确的。 JSON == JavaScript 对象表示法。所以,我的意思是 OP 用 JSON 表达的对象。现在开心吗?
标签: javascript jquery