我有下面的JSON数据,并且想使用php打印“ Page”和“ PageCount”,让我知道我该怎么做。
{
"response": {
"Page": 1,
"PageCount": 1,
"RecordsSent": 1,
"RecordsFound": 1,
"Stock": [
{
"Colour": "OFF WHITE",
"Size": "S",
"Style": "A0000001",
"Article": "FLORAL 1",
"Size_Range": "S - 3XL",
"In_Stock": 58,
"Demand": 0,
"Supply": 2,
"Sell_Price9": 0
}
]
}
}
我如何仅打印“页面”和“ PageCount”?
答案 0 :(得分:0)
您可以解析它,如下所示:Live Demo
<?php
$json = '{
"response": {
"Page": 1,
"PageCount": 1,
"RecordsSent": 1,
"RecordsFound": 1,
"Stock": [
{
"Colour": "OFF WHITE",
"Size": "S",
"Style": "A0000001",
"Article": "FLORAL 1",
"Size_Range": "S - 3XL",
"In_Stock": 58,
"Demand": 0,
"Supply": 2,
"Sell_Price9": 0
}
]
}
}';
$decoded_json = json_decode($json);
echo "Page: ".$decoded_json->response->Page."<br/>";
echo "Page Count: ".$decoded_json->response->PageCount;
答案 1 :(得分:0)
您可以尝试以下代码:
$content = '{
"response": {
"Page": 1,
"PageCount": 1,
"RecordsSent": 1,
"RecordsFound": 1,
"Stock": [
{
"Colour": "OFF WHITE",
"Size": "S",
"Style": "A0000001",
"Article": "FLORAL 1",
"Size_Range": "S - 3XL",
"In_Stock": 58,
"Demand": 0,
"Supply": 2,
"Sell_Price9": 0
}
]
}
}';
$data = json_decode($content, true);
echo "Page:", $data['response']['Page'], "\n";
echo "PageCount:", $data['response']['PageCount'];
json_decode
第二个参数是
Assoc
-为TRUE时,返回的对象将转换为关联数组。
这意味着,如果assoc = false或不被设置,则表明您拥有对象:
echo "Page:", $data->response->Page, "\n";
echo "PageCount:", $data->response->PageCount;