JSON:无法读取未定义的属性“0”

时间:2015-07-17 05:30:06

标签: javascript php json html-table

我正在尝试将JSON数据解析为html表,但我得到了:

  

“无法读取未定义的属性'0'

<?php

$idmatchs = "bGdzkiUVu,bCrAvXQpO,b4I6WYnGB,bMgwck80h";

$exploded = explode(",", $idmatchs);
$count = count($exploded);

?>

<script>
$.get( "obte2.php?id=f_1_0_-3_es_1", function ( data ) {
    var json = eval ("(" + data + ")");

    var matchids = "<?php echo $idmatchs ?> ";
    var ids =  matchids.split(",");

    for (i=0; i < ids.length; i++) {
        $( "#"+ids[i] + " #est" ).html( json.ids[i][0].EST );   
    }

});

</script>


<table class="tg">

 <?php

 for( $i= 0 ; $i < $count ; $i++ ){

    echo '<tr id="'.$exploded[$i].'">';
    echo '<td id="est"></td>';
    echo '</tr>';

  }

 ?>
 </table>

Chrome突出显示此行:

$( "#"+ids[i] + " #est" ).html( json.ids[i][0].EST );

事实上,如果我为任何id替换ids [i],代码就可以完美运行。

$( "#"+ids[i] + " #est" ).html( json.bGdzkiUVu[0].EST );

JSON文件是这样的:

{
"bGdzkiUVu": [
    {
        "INI": "1437150600",
        "EST": "PROG",
        "AC": "1",
        "LCL": "Amberg",
        "AX": "0",
        "LIN": "AMB",
        "AE": "Amberg",
        "WU": "amberg",
        "OA": "team",
        "WN": "MEM",
        "VST": "Memmingen",
        "WV": "memmingen",
        "OB": "team",
        "AN": "y"
    }
],
"bCrAvXQpO": [
    {
        "INI": "1437150600",
        "EST": "PROG",
        "AC": "1",
        "LCL": "SpVgg Bayreuth",
        "AX": "0",
        "WN": "SCH",
        "VST": "Schalding",
        "WV": "schalding",
        "OB": "team",
        "LIN": "SPV",
        "AE": "SpVgg Bayreuth",
        "WU": "spvgg-bayreuth",
        "OA": "team",
        "AN": "y"
    }
],
"b4I6WYnGB": [
    {
        "INI": "1437152400",
        "EST": "PROG",
        "AC": "1",
        "LCL": "1860 Munich II",
        "AX": "0",
        "LIN": "186",
        "AE": "1860 Munich II",
        "WU": "1860-munich",
        "OA": "team",
        "WN": "BUR",
        "VST": "Burghausen",
        "WV": "burghausen",
        "OB": "team",
        "AN": "y"
    }
],
"bMgwck80h": [
    {
        "INI": "1437152400",
        "EST": "PROG",
        "AC": "1",
        "LCL": "Buchbach",
        "AX": "0",
        "LIN": "BUC",
        "AE": "Buchbach",
        "WU": "buchbach",
        "OA": "team",
        "WN": "RAI",
        "VST": "Rain/Lech",
        "WV": "rain-lech",
        "OB": "team",
        "AN": "y"
    }
]
}

1 个答案:

答案 0 :(得分:0)

当您正在寻找ids[i]时,您要求JS查找ids[0]字面评估为ids[1]ids.bGdzkiUVu的{​​{1}}。您需要按键查找它们,而不是使用索引。

例如:

Object.keys(ids).forEach(function (key) {
    // do something with ids[key]
});