解析杰森对象

时间:2017-11-04 01:00:16

标签: php json

我有一个像下面这样的json对象。我需要找到并加载"数据"每个变量的值,以便进一步处理。我在这里经历了一些基本的PHP示例,但是没有用。非常感谢任何帮助。

{
"dataset":
    {


    "id":9775409,
    "dataset_code":"AAPL",
    "database_code":"WIKI",
    "name":"Apple Inc (AAPL) Prices, Dividends, Splits and Trading Volume",
    "description":"End of day open, high, low, close and volume, dividends and splits, and split/dividend adjusted open, high, low close and volume for Apple Inc. (AAPL). Ex-Dividend is non-zero on ex-dividend dates. Split Ratio is 1 on non-split dates. Adjusted prices are calculated per CRSP (\u003ca href=\"http://www.crsp.com/products/documentation/crsp-calculations\" rel=\"nofollow\" target=\"blank\"\u003ewww.crsp.com/products/documentation/crsp-calculations\u003c/a\u003e)\r\n\r\n\u003cp\u003eThis data is in the public domain. You may copy, distribute, disseminate or include the data in other products for commercial and/or noncommercial purposes.\u003c/p\u003e\r\n\u003cp\u003eThis data is part of Quandl's Wiki initiative to get financial data permanently into the public domain. Quandl relies on users like you to flag errors and provide data where data is wrong or missing. Get involved: \u003ca href=\"mailto:connect@quandl.com\" rel=\"nofollow\" target=\"blank\"\u003econnect@quandl.com\u003c/a\u003e",
    "refreshed_at":"2017-11-03T21:50:44.247Z",
    "newest_available_date":"2017-11-03",
    "oldest_available_date":"1980-12-12",
    "column_names":["Date","Open","High","Low","Close","Volume","Ex-Dividend","Split Ratio","Adj. Open","Adj. High","Adj. Low","Adj. Close","Adj. Volume"],
    "frequency":"daily",
    "type":"Time Series",
    "premium":false,"
    limit":null,
    "transform":null,
    "column_index":null,
    "start_date":"2017-11-03",
    "end_date":"2017-11-03",
    "data":[["2017-11-03",174.0,174.26,171.12,172.5,58683826.0,0.0,1.0,174.0,174.26,171.12,172.5,58683826.0]],
    "collapse":null,
    "order":"asc",
    "database_id":4922}


}

2 个答案:

答案 0 :(得分:0)

您可以像处理数组一样处理json对象;您可以遍历json或只调用单个文件。

<?php
$json_source = ''; # your json source

# two parameters:
# 1st - the source of the json file
# 2nd - if set to true it will set the array as an associative array
$json = json_decode($json_resource, true);

print_r($json['database']['data']);

详细了解json_decode check out the manual

这将从数据部分输出json值。要为每个变量赋值,可以遍历json:

foreach( $json['database']['data'] as $value )
{
    $data[] = $value;
}

这将创建一个名为$data的新数组。你知道会有这样的输出:

Array
(
    [0] => 2017-11-03
    [1] => ...
)

希望有所帮助!

答案 1 :(得分:0)

以下是最终代码:

<?php
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
   header("Cache-Control: post-check=0, pre-check=0", false);
   header("Pragma: no-cache");
  $url ='https://www.quandl.com/api/v3/datasets/WIKI/CNK.json?start_date=2017-11-03&end_date=2017-11-03&order=asc&transformation=rdiff&api_key=xxxx';
   $content = file_get_contents($url);
   $json = json_decode($content, true);
   $name = $json['dataset']['name'];
   $str_pos = strpos($name,"(");
   $closing_price = $json['dataset']['data'];
   echo 'Name '.substr($name,0, $str_pos).'<br/>';
   echo 'Closing price '.$closing_price[0][4].'<br/>';
?>