简体   繁体   English

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

[英]JSON: Cannot read property '0' of undefined

I'm trying to parse JSON data to a html table, but I get: 我正在尝试将JSON数据解析为html表,但得到:

"Cannot read property '0' of undefined" “无法读取未定义的属性'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 highlights this line: Chrome突出显示以下行:

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

In fact, if I replace ids[i] for any id, the code works perfectly. 实际上,如果我将ids [i]替换为任何id,则代码可以完美运行。

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

The JSON file is this: 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"
    }
]
}

You're asking JS to look for ids[i] which literally evaluates to ids[0] or ids[1] when you're actually looking for ids.bGdzkiUVu . 您要JS查找实际上要查找ids.bGdzkiUVu时的ids[i] ,其值实际上是ids[0]ids[1] Rather than using the index, you need to be looking them up by key. 而不是使用索引,您需要按键查找它们。

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM