递归遍历
删除的话可以用delete xx.xx
删除的话可以用delete xx.xx
解决方案 »
- 存储在数据库中流程信息怎么展示在页面上
- javascript的一个关于循环执行的问题
- 菜鸟问题:实现点击展开,再点击隐藏遇到的小问题
- 请教js能否模拟点击flash里面的按钮?
- MultiChosePerson?event=ShowOrgList&showSelect=yes&orgID="+linkPath+"&showoption=showrole
- 根据状态显示不同的div
- 如何用正则表达式判断字符串内的特殊字符是否符合要求?
- 文件上传问题,高手来,在线等.....
- 关于IE的打印问题
- 高分求得到当前文件夹下的所有文件名或文件夹名称的写法?
- 怎么判断 当div里面的内容 超出浏览器的可视区域 div出现滚动条
- 关于append()方法
给个代码你看看,没考虑优化问题,但是遍历子孙对象的属性特别有用function find(obj)
{
for(var i in obj)
{
if(obj[i]!=null&&typeof(obj[i])!="function"&&typeof(obj[i])=="object")
{
find(obj[i]);
}
//这里添加你需要属性操作的代码
}}
find(json);至于删除对象属性,使用delete运算符就可以了,删除指定的
我把全部代码贴出来吧
<script type="text/javascript">
function showJSON() {
var json={
"name":"zhansan",
"age":23,
"address":{
"city":"beijing",
"year":["气态流出物月报",0,1],
"gas":{
"gasSheet":"气态流出物月报",
"H_adjust":1
},
"time":{
"year": ["气态流出物月报",0,1],
"start":[1,"~"],
"duration":31
}
},
"units":{"title":"function"}, //怎么判断它是相对于根的子节点?
"student":[13,"s1","s2"]
}
var root="json";
var count=0;
var flag=false;
forTree=function(json){
for(var i in json){
if(typeof json[i] == "object"){ //有子节点
if(json[i].length >=0 ){ //数组
var nodes;
if(flag){
nodes=root+"."+i;
}else{
nodes="json."+i;
}
for(var l=0;l<json[i].length;l++){
alert(nodes+"["+l+"]"+" : "+json[i][l]);
}
}else{ //是对象
flag=true;
//iteration=true;
if(count > 0){ //迭代过一次,root截掉最后一个节点,接上新节点
root=root.substring(0,root.lastIndexOf("."))+"."+i;
}else{ //没有迭代过,root就用上一次的
root=root+"."+i;
}
forTree(json[i]);
flag=false;
count++;
forTree(json);
}
</script>
<body>
<input type="button" onclick="showJSON()" value="ShowJson" />
</body>
现在问题是“"units":{"title":"function"}”之前的都没有问题,但是到了这里,就打印“json.address.units.title : function”,这里本该是打印“json.units.title : function”的,然后下一个节点也没有问题,打印的是“json.student[0] : 13”...这该怎么解决呢?
我把全部代码贴出来吧
<script type="text/javascript">
function showJSON() {
var json={
"name":"zhansan",
"age":23,
"address":{
"city":"beijing",
"year":["气态流出物月报",0,1],
"gas":{
"gasSheet":"气态流出物月报",
"H_adjust":1
},
"time":{
"year": ["气态流出物月报",0,1],
"start":[1,"~"],
"duration":31
}
},
"units":{"title":"function"}, //怎么判断它是相对于根的子节点?
"student":[13,"s1","s2"]
}
var root="json";
var count=0;
var flag=false;
forTree=function(json){
for(var i in json){
if(typeof json[i] == "object"){ //有子节点
if(json[i].length >=0 ){ //数组
var nodes;
if(flag){
nodes=root+"."+i;
}else{
nodes="json."+i;
}
for(var l=0;l<json[i].length;l++){
alert(nodes+"["+l+"]"+" : "+json[i][l]);
}
}else{ //是对象
flag=true;
//iteration=true;
if(count > 0){ //迭代过一次,root截掉最后一个节点,接上新节点
root=root.substring(0,root.lastIndexOf("."))+"."+i;
}else{ //没有迭代过,root就用上一次的
root=root+"."+i;
}
forTree(json[i]);
flag=false;
count++;
forTree(json);
}
</script>
<body>
<input type="button" onclick="showJSON()" value="ShowJson" />
</body>
现在问题是“"units":{"title":"function"}”之前的都没有问题,但是到了这里,就打印“json.address.units.title : function”,这里本该是打印“json.units.title : function”的,然后下一个节点也没有问题,打印的是“json.student[0] : 13”...这该怎么解决呢?
代码贴错了,应该是这样:
<script type="text/javascript">
function showJSON() {
var json={
"name":"zhansan",
"age":23,
"address":{
"city":"beijing",
"year":["气态流出物月报",0,1],
"gas":{
"gasSheet":"气态流出物月报",
"H_adjust":1
},
"time":{
"year": ["气态流出物月报",0,1],
"start":[1,"~"],
"duration":31
}
},
"units":{"title":"function"}, //怎么判断它是相对于根的子节点?
"student":[13,"s1","s2"]
}
//alert(json["address"]["year"]);
var root="json";
var count=0;
var flag=false;
forTree=function(json){
for(var i in json){
if(typeof json[i] == "object"){ //有子节点
if(json[i].length >=0 ){ //数组
var nodes;
if(flag){
nodes=root+"."+i;
}else{
nodes="json."+i;
}
for(var l=0;l<json[i].length;l++){
alert(nodes+"["+l+"]"+" : "+json[i][l]);
}
}else{ //是对象
flag=true;
//iteration=true;
if(count > 0){ //迭代过一次,root截掉最后一个节点,接上新节点
root=root.substring(0,root.lastIndexOf("."))+"."+i;
}else{ //没有迭代过,root就用上一次的
root=root+"."+i;
}
forTree(json[i]);
flag=false;
count++;
}
}else{
alert(root+"."+i+" : "+json[i]);
}
}
}
forTree(json);
}
</script>
<body>
<input type="button" onclick="showJSON()" value="ShowJson" />
</body>
var json = {
"name": "zhansan",
"age": 23,
"address": {
"city": "beijing",
"year": [{ test: "气态流出物月报" }, [0, [1, 1]], 1],
"gas": {
"gasSheet": "气态流出物月报",
"H_adjust": 1
},
"time": {
"year": ["气态流出物月报", 0, 1],
"start": [1, "~"],
"duration": 31
}
},
"units": { "title": "function" }, //怎么判断它是相对于根的子节点?
"student": [13, "s1", "s2"]
}
function RecursionJson(json, path) {
var isArray = json.length;
for (var attr in json)
if (typeof json[attr] == 'object') {
if (json[attr].length) {
for (var j = 0; j < json[attr].length; j++) {
if (typeof json[attr][j] == 'object')
RecursionJson(json[attr][j], path + '.' + attr + '[' + j + ']'); //不是普通类型,递归遍历
else alert(path + (isArray ? '[' : '.') + attr + (isArray ? ']' : '') + '[' + j + ']:' + json[attr][j]);
}
}
else RecursionJson(json[attr], path + "." + attr); //递归遍历
}
else alert(path + (isArray ? '[' : '.') + attr + (isArray ? ']' : '') + ':' + json[attr]);
} RecursionJson(json, "json");
}