当有多个结果时,json_decode()

时间:2015-05-21 23:24:40

标签: php json decode

我试图了解如何从json结果中获取SKU(以及另外的ID)。当只有一组名为SKU的值时,我可以这样做,但是当有多个值时,我花了好几个小时试图了解如何执行此操作。

以下是我的json返回的示例



{
    "variants": [
        {
            "id": 6852445,
            "name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
            "sku": "VK7i-S-SHX7",
        },
        {
            "id": 6852388,
            "name": "ikan Flyweight DSLR",
            "sku": "ELE-FLWDSLR",
        },
        {
            "id": 6838367,
            "name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
            "sku": "AO-ATOMSUN001",
        },
                ]
}




我目前有这个代码(我是新手)。我想要做的是使sku和ID都变量,但我只是碰到了一堵砖墙。目前我得到

警告:在第15行的/home/pearingc/public_html/returns/test2.php中为foreach()提供的参数无效



<?php

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Bearer *my token details* ")
    )
);

$url = "http://api.tradegecko.com/variants/";
$data = file_get_contents($url, false, $context);

$json = json_decode($data, true);

$product = $json{'variant'}->{'sku'};
foreach ($product['sku'] as $sku) {
print $sku;
}

?>
&#13;
&#13;
&#13;

编辑:这就是var_dump($ json);给我

&#13;
&#13;
object(stdClass)#1 (2) { ["variants"]=> array(100) { [0]=> object(stdClass)#2 (42) { ["id"]=> int(6852445) ["created_at"]=> string(24) "2015-05-20T10:09:09.629Z" ["updated_at"]=> string(24) "2015-05-20T10:10:40.351Z" ["product_id"]=> int(1991122) ["default_ledger_account_id"]=> NULL ["buy_price"]=> string(5) "404.0" ["committed_stock"]=> string(1) "0" ["incoming_stock"]=> string(1) "0" ["composite"]=> bool(true) ["description"]=> NULL ["is_online"]=> bool(false) ["keep_selling"]=> bool(false) ["last_cost_price"]=> NULL ["manage_stock"]=> bool(true) ["max_online"]=> NULL ["moving_average_cost"]=> NULL ["name"]=> string(49) "Ikan VK7i 7" LCD Monitor for Sony L with sun hood" ["online_ordering"]=> bool(false) ["opt1"]=> NULL ["opt2"]=> NULL ["opt3"]=> NULL ["position"]=> int(6) ["product_name"]=> string(33) "ikan 7" HDMI Monitor W/ IPS Panel" ["product_status"]=> string(6) "active" ["product_type"]=> string(8) "Monitors" ["retail_price"]=> NULL ["sellable"]=> bool(true) ["sku"]=> string(11) "VK7i-S-SHX7" ["status"]=> string(6) "active" ["stock_on_hand"]=> string(1) "0" ["supplier_code"]=> NULL ["taxable"]=> bool(true) ["upc"]=> NULL ["weight"]=> NULL ["wholesale_price"]=> NULL ["image_ids"]=> array(0) { } ["variant_prices"]=> array(1) { [0]=> object(stdClass)#3 (2) { ["price_list_id"]=> string(3) "buy" ["value"]=> string(5) "404.0" } } ["locations"]=> array(1) { [0]=> object(stdClass)#4 (6) { ["location_id"]=> int(16377) ["stock_on_hand"]=> string(1) "0" ["committed"]=> string(1) "0" ["incoming"]=> NULL ["bin_location"]=> NULL ["reorder_point"]=> NULL } } ["prices"]=> object(stdClass)#5 (1) { ["buy"]=> string(5) "404.0" } ["stock_levels"]=> object(stdClass)#6 (1) { ["16377"]=> string(3) "0.0" } ["committed_stock_levels"]=> object(stdClass)#7 (1) { ["16377"]=> string(3) "0.0" } ["incoming_stock_levels"]=> object(stdClass)#8 (0) { } } 
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

我认为你的JSON格式不正确。注意:我在sku之后删除了逗号。试试这个:

$data = '{
"variants": [
    {
        "id": 6852445,
        "name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
        "sku": "VK7i-S-SHX7"
    },
    {
        "id": 6852388,
        "name": "ikan Flyweight DSLR",
        "sku": "ELE-FLWDSLR"
    },
    {
        "id": 6838367,
        "name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
        "sku": "AO-ATOMSUN001"
    }
]
}';

之后:

$json = json_decode($data);
foreach ($json->variants as $row) {
    print $row->sku;
}

答案 1 :(得分:0)

<?php

$json = '{
    "variants": [
        {
            "id": 6852445,
            "name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
            "sku": "VK7i-S-SHX7"
        },
        {
            "id": 6852388,
            "name": "ikan Flyweight DSLR",
            "sku": "ELE-FLWDSLR"
        },
        {
            "id": 6838367,
            "name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
            "sku": "AO-ATOMSUN001"
        }
    ]
}';

foreach(json_decode($json,true)['variants'] as $item) {
  echo $item['sku'] . "<br />";
}
?>

凝聚了一点。