0

我正在努力获取正确的值

我正在尝试获取以下内容

packages>items 然后循环遍历 id

{"id":1,"title":"Barnsley","slug":"barnsley","packages":[{"id":1,"title":"Group PACKAGE 1","items" 
 {"id":9,"title":"Guiness","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":10,"title":"John Smith","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":22,"title":"Chicken Cheese and Bacon Melt","band_a":4.5,"band_b":4.75,"band_c":5.0,"band_d":5.25,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null}],"additional_items":null,"event_types":null}]}
4

1 回答 1

1

您的 JSON 无效。您缺少两个:[声明 items 数组的字符。修复 JSON(在 jsonlint.com 上查看)后,您可以像这样迭代项目 ID:

$obj = json_decode($str);

foreach($obj->packages[0]->items as $item)
{
    echo $item->id;
}

如果可能有多个包,您也可以迭代它们:

foreach($obj->packages as $package)
{
    foreach($package->items as $item)
    {
        echo $item->id;
    }
}
于 2013-10-24T16:04:29.890 回答