从对象内部打印值

时间:2015-05-25 16:07:36

标签: php json

我确实遇到了麻烦,试图弄清楚为什么这不起作用,看起来应该很容易,但似乎无法得到它。

我要做的就是抓住sku字段(下面是VBP-01)。



{
    "variants": [
        {
            "id": 2314578,
            "created_at": "2014-07-29T07:22:18.921Z",
            "updated_at": "2015-05-21T15:42:42.136Z",
            "product_id": 1188647,
            "default_ledger_account_id": null,
            "buy_price": "124.0",
            "committed_stock": "0",
            "incoming_stock": "3",
            "composite": false,
            "description": null,
            "is_online": false,
            "keep_selling": false,
            "last_cost_price": "124.0",
            "manage_stock": true,
            "max_online": null,
            "moving_average_cost": "124",
            "name": "Lanparte battery pinch VBP-01",
            "online_ordering": false,
            "opt1": null,
            "opt2": null,
            "opt3": null,
            "position": 1,
            "product_name": "Lanparte V-Mount Battery Pinch",
            "product_status": "active",
            "product_type": null,
            "retail_price": "0.0",
            "sellable": true,
            "sku": "VBP-01",
            "status": "active",
            "stock_on_hand": "1",
            "supplier_code": "VBP-01",
            "taxable": true,
            "upc": null,
            "weight": null,
            "wholesale_price": "0.0",
            "image_ids": [],
            "variant_prices": [
                {
                    "price_list_id": "buy",
                    "value": "124.0"
                },
                {
                    "price_list_id": "retail",
                    "value": "0.0"
                },
                {
                    "price_list_id": "wholesale",
                    "value": "0.0"
                }
            ],
            "locations": [
                {
                    "location_id": 16377,
                    "stock_on_hand": "1",
                    "committed": null,
                    "incoming": "3",
                    "bin_location": null,
                    "reorder_point": 3
                }
            ],
            "prices": {
                "buy": "124.0",
                "retail": "0.0",
                "wholesale": "0.0"
            },
            "stock_levels": {
                "16377": "1.0"
            },
            "committed_stock_levels": {},
            "incoming_stock_levels": {
                "16377": "3.0"
            }
        }
    ],
    "meta": {
        "total": 1
    }
}




目前我使用以下代码没有运气



$url = "https://api.tradegecko.com/variants?sku=VBP-01";
$data = file_get_contents($url, false, $context);

$json = json_decode($data);

print $json->{'variant'}->{'sku'};




我做错了什么?

1 个答案:

答案 0 :(得分:0)

只需

echo $json->{'variants'}[0]->{'sku'};

循环

foreach ($json->variants as $variants) {
    echo $variants->sku;
}