首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当有多个结果时,json_decode()

当有多个结果时,json_decode()
EN

Stack Overflow用户
提问于 2015-05-21 23:24:39
回答 2查看 1.6K关注 0票数 1

我正试图弄清楚如何从json结果中获得SKU(以及ID)。当只有一组名为SKU的值时,我可以这样做,但是我花了几个小时来了解如何在有多个值的情况下做到这一点。

下面是我的json返回的示例

代码语言:javascript
复制
{
    "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/珠光c/public_html/ line /test2.php中的foreach()提供的无效参数

代码语言:javascript
复制
<?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;
}

?>

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

代码语言:javascript
复制
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) { } } 

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-21 23:36:00

我觉得你有畸形的JSON。注意:我在sku之后删除了逗号。试试这个:

代码语言:javascript
复制
$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"
    }
]
}';

在那之后:

代码语言:javascript
复制
$json = json_decode($data);
foreach ($json->variants as $row) {
    print $row->sku;
}
票数 3
EN

Stack Overflow用户

发布于 2015-05-21 23:57:58

代码语言:javascript
复制
<?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 />";
}
?>

浓缩了一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30385989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档