【问题标题】:Iterate over particular json遍历特定的 json
【发布时间】:2012-02-28 13:58:54
【问题描述】:

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

    {
        EDITED: I DID HAVE AN ERROR, SORRY
    }

我正在寻找没有任何 js 框架的解决方案。 ¿如何获得,例如,

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

?

谢谢


更多信息:

我之前的代码确实有错误!!!对不起

功能:填充两个选择,“select1”和“select2”,option=Category optgroup=CategoryParent

$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);

所以我得到了类似的东西:

    {
        "select1":  {  "optgroup1":[{"v":"1","l":"option1"},{"v":"2","l":"option2"}],
                       "optgroup2":[{"v":"3","l":"option1"},{"v":"4","l":"option2"}],
                       "optgroup3":[{"v":"5","l":"option1"},{"v":"6","l":"option2"}]
                    },
        "select2":  {  " (...)
    }

//optgroup 是 parentCategory //options 是类别 no-parents

【问题讨论】:

  • 您必须提供更多信息。您如何从 JavaScript 访问数据? IE。您是通过 Ajax 检索,还是您的 PHP 脚本实际生成 JavaScript 并将数据插入其中?一般来说,你不能真正遍历任何 JSON 数据,因为它只是一个文本表示。不过,您可以将其转换为 JavaScript 对象,然后在您认为合适的时候简单地访问其属性。

标签: javascript iteration json


【解决方案1】:

使用递归:

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

【讨论】:

  • 非常有用。你帮了我很多!非常感谢
【解决方案2】:

你的意思是:

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

等等?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多