PHP:访问复杂json中的数据

时间:2018-04-26 15:09:59

标签: php json api joomla

我目前正在开发一个Joomla模块,作为已停产的雅虎财务的替代方案,我决定使用来自ietrading的API,例如,它提供的数据如下所示:

示例提取: print_r的可读输出($ json):

    {
    "AAPL": {
      "quote": {
        "symbol": "AAPL",
        "companyName": "Apple Inc.",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "tops",
        "open": 162.62,            
        ...
        "week52High": 183.5,
        "week52Low": 142.2,
        "ytdChange": -0.04610909853958216
      }
    },
    "FB": {
      "quote": {
        "symbol": "FB",
        "companyName": "Facebook Inc.",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "tops",
        "open": 160.07,
        "openTime": 1524663000827,
        "close": 159.69,
        "closeTime": 1524686400183,
        "high": null,
        "low": null,
        "latestPrice": 173.19,
        ...
        "week52High": 195.32,
        "week52Low": 144.4216,
        "ytdChange": -0.11977731231396754
      }
    }
}

对我来说,问题是如何使用输出访问每个索引中的报价数据,如下所示:

AAPL : Apple Inc.
FB : Facebook Inc.

唯一有效的时间是我做这样的事情:

$test_data = file_get_contents('data.json');    
$json = json_decode($test_data,true);

$jsonData[] = "";

foreach($json as $item){
    $allItems[] = $item;
}

var_dump($allItems);

for ($i=0; $i < COUNT($allItems); $i++) {
    $jsonData[]= $allItems[$i]['quote'];
    echo $jsonData[$i+1]['companyName'].' - from all for<br/>';
}
echo "<h2>Testing</h2>";
echo $jsonData[1]['symbol'].' : '.$jsonData[1]['companyName'].'<br/>';
echo $jsonData[2]['symbol'].' : '.$jsonData[2]['companyName'].'<br/>';

我相信有一种优化方式可以做到这一点。

建议

print_r($ json)

`'Array ( [AAPL] => Array ( [quote] => Array ( [symbol] => AAPL [companyName] => Apple Inc. [primaryExchange] => Nasdaq Global Select [sector] => Technology [calculationPrice] => tops [open] => 162.62 [openTime] => 1524663000142 [close] => 163.65 [closeTime] => 1524686400487 [high] => [low] => [latestPrice] => 164.57 [latestSource] => IEX real time price [latestTime] => 9:35:46 AM [latestUpdate] => 1524749746099 [latestVolume] => 1354291 [iexRealtimePrice] => 164.57 [iexRealtimeSize] => 100 [iexLastUpdated] => 1524749746099 [delayedPrice] => 164.28 [delayedPriceTime] => 1524748852180 [previousClose] => 163.65 [change] => 0.92 [changePercent] => 0.00562 [iexMarketPercent] => 0.01142 [iexVolume] => 15466 [avgTotalVolume] => 32759551 [iexBidPrice] => 164.61 [iexBidSize] => 100 [iexAskPrice] => 165.07 [iexAskSize] => 100 [marketCap] => 835030319410 [peRatio] => 16.91 [week52High] => 183.5 [week52Low] => 142.2 [ytdChange] => -0.046109098539582 ) ) [FB] => Array ( [quote] => Array ( [symbol] => FB [companyName] => Facebook Inc. [primaryExchange] => Nasdaq Global Select [sector] => Technology [calculationPrice] => tops [open] => 160.07 [openTime] => 1524663000827 [close] => 159.69 [closeTime] => 1524686400183 [high] => [low] => [latestPrice] => 173.19 [latestSource] => IEX real time price [latestTime] => 9:35:55 AM [latestUpdate] => 1524749755278 [latestVolume] => 13276538 [iexRealtimePrice] => 173.19 [iexRealtimeSize] => 100 [iexLastUpdated] => 1524749755278 [delayedPrice] => 172.53 [delayedPriceTime] => 1524748852406 [previousClose] => 159.69 [change] => 13.5 [changePercent] => 0.08454 [iexMarketPercent] => 0.02455 [iexVolume] => 325939 [avgTotalVolume] => 48329785 [iexBidPrice] => 173.04 [iexBidSize] => 200 [iexAskPrice] => 173.22 [iexAskSize] => 100 [marketCap] => 502140357915 [peRatio] => 28.16 [week52High] => 195.32 [week52Low] => 144.4216 [ytdChange] => -0.11977731231397 ) ) )'`

1 个答案:

答案 0 :(得分:2)

这是一个简短的方法:

在这个例子中,我使用的是foreach循环而不是带索引的循环。这使您可以按对象迭代数组对象并根据需要过滤数据。

<?php
        //Enter your code here, enjoy!
 $data = ' {
    "AAPL": {
      "quote": {
        "symbol": "AAPL",
        "companyName": "Apple Inc.",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "tops",
        "open": 162.62,            
        "week52High": 183.5,
        "week52Low": 142.2,
        "ytdChange": -0.04610909853958216
      }
    },
    "FB": {
      "quote": {
        "symbol": "FB",
        "companyName": "Facebook Inc.",
        "primaryExchange": "Nasdaq Global Select",
        "sector": "Technology",
        "calculationPrice": "tops",
        "open": 160.07,
        "openTime": 1524663000827,
        "close": 159.69,
        "closeTime": 1524686400183,
        "high": null,
        "low": null,
        "latestPrice": 173.19,

        "week52High": 195.32,
        "week52Low": 144.4216,
        "ytdChange": -0.11977731231396754
      }
    }
}';

$json = json_decode($data,true);

foreach($json as $key  => $object){
    echo $key ." : ". $object['quote']['companyName']."</br>";
}

结果:

  

AAPL:Apple Inc。

     

FB:Facebook Inc。

我希望能帮助你。