从Yahoo

时间:2016-01-26 19:40:18

标签: javascript json ajax

我正在使用此API链接:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDEUR%22,%20%22USDJPY%22,%20%22USDBGN%22,%20%22USDCZK%22,%20%22USDDKK%22,%20%22USDGBP%22,%20%22USDHUF%22,%20%22USDLTL%22,%20%22USDLVL%22,%20%22USDPLN%22,%20%22USDRON%22,%20%22USDSEK%22,%20%22USDCHF%22,%20%22USDNOK%22,%20%22USDHRK%22,%20%22USDRUB%22,%20%22USDTRY%22,%20%22USDAUD%22,%20%22USDBRL%22,%20%22USDCAD%22,%20%22USDCNY%22,%20%22USDHKD%22,%20%22USDIDR%22,%20%22USDILS%22,%20%22USDINR%22,%20%22USDKRW%22,%20%22USDMXN%22,%20%22USDMYR%22,%20%22USDNZD%22,%20%22USDPHP%22,%20%22USDSGD%22,%20%22USDTHB%22,%20%22USDZAR%22,%20%22USDISK%22%29&env=store://datatables.org/alltableswithkeys

但似乎这不是选择具有特定id的元素的方法。

请检查链接,我的问题是内部结果 - >费率元素具有不同的ID,所以现在我只想要USDEUR索引,我尝试了类似的东西

<script>
            setInterval(function(){ 
                //somehow convert xml to json
                $.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDEUR%22,%20%22USDJPY%22,%20%22USDBGN%22,%20%22USDCZK%22,%20%22USDDKK%22,%20%22USDGBP%22,%20%22USDHUF%22,%20%22USDLTL%22,%20%22USDLVL%22,%20%22USDPLN%22,%20%22USDRON%22,%20%22USDSEK%22,%20%22USDCHF%22,%20%22USDNOK%22,%20%22USDHRK%22,%20%22USDRUB%22,%20%22USDTRY%22,%20%22USDAUD%22,%20%22USDBRL%22,%20%22USDCAD%22,%20%22USDCNY%22,%20%22USDHKD%22,%20%22USDIDR%22,%20%22USDILS%22,%20%22USDINR%22,%20%22USDKRW%22,%20%22USDMXN%22,%20%22USDMYR%22,%20%22USDNZD%22,%20%22USDPHP%22,%20%22USDSGD%22,%20%22USDTHB%22,%20%22USDZAR%22,%20%22USDISK%22%29&env=store://datatables.org/alltableswithkeys', function(data) {
                var USD = 1/data.query.results.rate['USDEUR'].Rate * 1.955
                document.getElementById('output-value').innerHTML = USD * document.getElementById('input-value').value;     
                });
             }, 500);
        </script>

1 个答案:

答案 0 :(得分:3)

如果您通过解码器运行URL(例如,PHP urldecode()功能,这就是我所做的,或者是在线提供的众多解码器之一),这将更容易理解。 URL只是这个的编码版本:

http://query.yahooapis.com/v1/public/yql?q=
select *
from yahoo.finance.xchange
where pair in ("USDEUR", "USDJPY", "USDBGN", "USDCZK", "USDDKK", 
    "USDGBP", "USDHUF", "USDLTL", "USDLVL", "USDPLN", "USDRON", 
    "USDSEK", "USDCHF", "USDNOK", "USDHRK", "USDRUB", "USDTRY", 
    "USDAUD", "USDBRL", "USDCAD", "USDCNY", "USDHKD", "USDIDR", 
    "USDILS", "USDINR", "USDKRW", "USDMXN", "USDMYR", "USDNZD", 
    "USDPHP", "USDSGD", "USDTHB", "USDZAR", "USDISK")
&env=store://datatables.org/alltableswithkeys

我添加了一些格式,以便明确发生了什么。它是一个YQL (Yahoo! Query Language)语句(实际上只是一个SQL SELECT语句),带有一对名称列表。

要获得所需的结果,只需从网址中删除其他对,例如:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDEUR%22%29&env=store://datatables.org/alltableswithkeys

响应(为便于阅读而格式化):

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2016-01-26T19:43:48Z" yahoo:lang="en-US">
    <results>
        <rate id="USDEUR">
            <Name>USD/EUR</Name>
            <Rate>0.9217</Rate>
            <Date>1/26/2016</Date>
            <Time>7:43pm</Time>
            <Ask>0.9218</Ask>
            <Bid>0.9217</Bid>
        </rate>
    </results>
</query>
<!-- total: 14 -->
<!-- main-4f1bf6c6-c3ce-11e5-a05e-56847afe9799 -->