Javascript 迭代特定的json

Javascript 迭代特定的json,javascript,iteration,json,Javascript,Iteration,Json,我需要在js(无框架)中迭代php中生成的json对象,其结构如下: { EDITED: I DID HAVE AN ERROR, SORRY } 我搜索一个没有任何js框架的解决方案。 ?例如,如何获得 lvl1a.lvl2a.a lvl1a.lvl2a.b lvl1a.lvl2b.a lvl1a.lvl2b.b ... ? 谢谢 更多信息: 我确实有一个错误,在上一个代码!!!对不起 功能:使用option=Category optgroup=Categ

我需要在js(无框架)中迭代php中生成的json对象,其结构如下:

    {
        EDITED: I DID HAVE AN ERROR, SORRY
    }
我搜索一个没有任何js框架的解决方案。 ?例如,如何获得

lvl1a.lvl2a.a
lvl1a.lvl2a.b
lvl1a.lvl2b.a
lvl1a.lvl2b.b
...
?

谢谢


更多信息:

我确实有一个错误,在上一个代码!!!对不起

功能:使用option=Category optgroup=CategoryParent填充两个select,“select1”和“select2”

$toJson=array();
$selectname=array("select1","select2");
for($i=0;$i<2;$i++){
    $sql="SELECT idCategory as v,name as l,(select name from categories where idCategory=C.idCategoryParent) as p FROM categories C WHERE idBanner".($i+1)." is NULL order by idCategoryParent, `order`";
    $result =$ocdb->query($sql);
    $nameIdx = "";
    while ($row=mysql_fetch_assoc($result)){
        $first=mysql_field_name($result, 0);
        $second=mysql_field_name($result,1);
        $third= mysql_field_name($result,2);            

        if($nameIdx!=$row[$third])
                $nameIdx=$row[$third];
        }
            if($row[$third]!=NULL){
                array_pop($row);
                $toJson[$selectname[$i]][$nameIdx][]=$row;
            }

    }

}
$inJson=json_encode($toJson);
//optgroup是parentCategory //选项是没有父母的类别你的意思是:

myObject.lvl1a.lvl2a[0].a
myObject.lvl1a.lvl2a[1].b

等等?

使用递归的方法:

function iterateObject(obj){
  var result = [];

  function recurse(obj){
     for (var l in obj){
      if(obj.hasOwnProperty(l)){
       result.push( l+ 
                    (!(obj[l] instanceof Object) 
                     ? ': '+obj[l] 
                     : ' -> object >' ));
       if (obj[l] instanceof Object){
          recurse(obj[l]);
       }
      }
     }
  }

  recurse(obj);
  return result;
}

var thing = {
    "lvl1a":  {  "lvl2a":[{"a":"xxxxx"},{"b":"xxxxx"}],
                 "lvl2b":[{"a":"xxxxx"},{"b":"xxxxx"}],
                 "lvl2c":[{"a":"xxxxx"},{"b":"xxxxx"}]
              },
    "lvl1b":  {  "lvl3a":[{"c":"xxxxx"},{"d":"xxxxx"}],
                 "lvl3b":[{"c":"xxxxx"},{"d":"xxxxx"}],
                 "lvl3c":[{"c":"xxxxx"},{"d":"xxxxx"}]
              }
};
console.log(iterateObject(thing).join('\n'));
//=> 
// lvl1a -> object >
// lvl2a -> object >
// 0 -> object >
// a: xxxxx
// 1 -> object >
// b: xxxxx
// ... etc

你必须提供更多的信息。您如何从JavaScript访问数据?也就是说,您是通过Ajax进行检索,还是您的PHP脚本实际生成JavaScript并将数据插入其中?一般来说,您无法真正遍历任何JSON数据,因为它只是一种文本表示。您可以将其转换为JavaScript对象,然后根据需要访问其属性。非常有用。你帮了我很多忙!非常感谢你
function iterateObject(obj){
  var result = [];

  function recurse(obj){
     for (var l in obj){
      if(obj.hasOwnProperty(l)){
       result.push( l+ 
                    (!(obj[l] instanceof Object) 
                     ? ': '+obj[l] 
                     : ' -> object >' ));
       if (obj[l] instanceof Object){
          recurse(obj[l]);
       }
      }
     }
  }

  recurse(obj);
  return result;
}

var thing = {
    "lvl1a":  {  "lvl2a":[{"a":"xxxxx"},{"b":"xxxxx"}],
                 "lvl2b":[{"a":"xxxxx"},{"b":"xxxxx"}],
                 "lvl2c":[{"a":"xxxxx"},{"b":"xxxxx"}]
              },
    "lvl1b":  {  "lvl3a":[{"c":"xxxxx"},{"d":"xxxxx"}],
                 "lvl3b":[{"c":"xxxxx"},{"d":"xxxxx"}],
                 "lvl3c":[{"c":"xxxxx"},{"d":"xxxxx"}]
              }
};
console.log(iterateObject(thing).join('\n'));
//=> 
// lvl1a -> object >
// lvl2a -> object >
// 0 -> object >
// a: xxxxx
// 1 -> object >
// b: xxxxx
// ... etc