循环时抛出一个json的问题

时间:2017-01-12 23:14:44

标签: php json

这让我疯了,

我试图循环抛出一个特定的json:

我尝试使用

json_encode - > throws警告:curl_close()期望参数1为资源,给定

为null

json_decode - >显示相同的文字

$ json-> {" SKU"}和$ json-> SKU显示相同的文字

我尝试用不同的网站验证json,并且都说json是正确的

我想只取这个价值SKU,价格,特价(包含所有内容)和数量

[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
    "Id": "423",
    "CategoryName": "Moda",
    "LevelDepth": "2",
    "Parent": null
}, {
    "Id": "960",
    "CategoryName": "Accesorios de Mujer",
    "LevelDepth": "3",
    "Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
    "StartDate": "2014-11-21 06:00:00",
    "EndDate": "2017-12-31 23:59:00",
    "Reduction": "20.000000",
    "ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
    "Id": "77",
    "Cover": "1",
    "Legend": "Divided correa Carrie",
    "url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
    "Id": "78",
    "Cover": "0",
    "Legend": "Divided correa Carrie",
    "url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
    "Id": "79",
    "Cover": "0",
    "Legend": "Divided correa Carrie",
    "url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
    "SKU": "001503280",
    "Price": 39.9,
    "FinalPrice": 16.3,
    "Default": "1",
    "IdImage": "77",
    "Attributes": [{
        "Name": "Color",
        "Value": "Naranja"
    }]
}, {
    "SKU": "001503281",
    "Price": 39.9,
    "FinalPrice": 39.9,
    "Default": "0",
    "IdImage": "78",
    "Attributes": [{
        "Name": "Color",
        "Value": "Blanco"
    }]
}, {
    "SKU": "001503282",
    "Price": 39.9,
    "FinalPrice": 39.9,
    "Default": "0",
    "IdImage": "79",
    "Attributes": [{
        "Name": "Color",
        "Value": "Negro"
    }]
}]

}]

非常感谢任何提示(我已经尝试了几乎每个堆栈上的答案)

1 个答案:

答案 0 :(得分:0)

我很确定你可以这样做:

<?php
        //Enter your code here, enjoy!
$text = <<<EOT
[{
    "SKU": "118075",
    "BrandName": "DIVIDED",
    "CategoryName": "Correas",
    "Categories": [{
        "Id": "423",
        "CategoryName": "Moda",
        "LevelDepth": "2",
        "Parent": null
    }, {
        "Id": "960",
        "CategoryName": "Accesorios de Mujer",
        "LevelDepth": "3",
        "Parent": "Moda"
    }],
    "Name": "Divided correa Carrie",
    "Description": "a",
    "DescriptionShort": "v",
    "MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
    "MetaTitle": "Divided Correa Carrie",
    "IsPublished": "1",
    "PackageWeight": "1.000000",
    "Price": 39.9,
    "FinalPrice": 16.3,
    "SpecialPrice": {
        "StartDate": "2014-11-21 06:00:00",
        "EndDate": "2017-12-31 23:59:00",
        "Reduction": "20.000000",
        "ReductionType": "amount"
    },
    "Quantity": "39",
    "DateAdd": "2015-09-24 11:26:08",
    "DateUpd": "2016-10-12 12:51:13",
    "Images": [{
        "Id": "77",
        "Cover": "1",
        "Legend": "Divided correa Carrie",
        "url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
    }, {
        "Id": "78",
        "Cover": "0",
        "Legend": "Divided correa Carrie",
        "url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
    }, {
        "Id": "79",
        "Cover": "0",
        "Legend": "Divided correa Carrie",
        "url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
    }],
    "FlatVariations": [{
        "SKU": "001503280",
        "Price": 39.9,
        "FinalPrice": 16.3,
        "Default": "1",
        "IdImage": "77",
        "Attributes": [{
            "Name": "Color",
            "Value": "Naranja"
        }]
    }, {
        "SKU": "001503281",
        "Price": 39.9,
        "FinalPrice": 39.9,
        "Default": "0",
        "IdImage": "78",
        "Attributes": [{
            "Name": "Color",
            "Value": "Blanco"
        }]
    }, {
        "SKU": "001503282",
        "Price": 39.9,
        "FinalPrice": 39.9,
        "Default": "0",
        "IdImage": "79",
        "Attributes": [{
            "Name": "Color",
            "Value": "Negro"
        }]
    }]
}]
EOT;

$decoded = json_decode($text);

# Index 0 because is an array
var_dump($decoded[0]->SKU);

您可以直接执行代码here

希望这有帮助。