使用javascript解析JSON URL

时间:2015-02-19 18:32:17

标签: javascript json api

我被困在json解析网址中。我没有从解析这个json url获得任何数据。我希望从这个json得到“问”和“出价”。 http://www.fxgrow.com/quotes/quotes.php

其结构是:

{
EURUSD: {
dir: 1,
ask: "1.13960",
bid: "1.13955"
},
USDJPY: {
dir: 1,
ask: "118.928",
bid: "118.925"
},
USDCHF: {
dir: 1,
ask: "0.94488",
bid: "0.94459"
},
GBPUSD: {
dir: 1,
ask: "1.54351",
bid: "1.54342"
},
AUDUSD: {
dir: 0,
ask: "0.77653",
bid: "0.77648"
},
NZDUSD: {
dir: 1,
ask: "0.75169",
bid: "0.75158"
},
GBPJPY: {
dir: 1,
ask: "183.564",
bid: "183.553"
},
EURGBP: {
dir: 1,
ask: "0.73836",
bid: "0.73829"
}
}

我的javascript代码是:

$.getJSON("http://www.fxgrow.com/quotes/quotes.php", function(data) {
    alert(data.msg);
});`

1 个答案:

答案 0 :(得分:0)

JSFIDDLE:http://jsfiddle.net/yumq1mzu/1/

只需克隆桌面上的chrome的快捷方式,然后在快捷方式属性中添加参数--disable-web-security,如下所示在chrome可执行文件路径的末尾添加:

"C:\Program Files\Google\Chrome\Application\chrome.exe" --disable-web-security

重新启动Chrome,并在代码中进行此更改:

$.getJSON("http://www.fxgrow.com/quotes/quotes.php", function(data) {
    alert(data.EURGBP.dir);   // alert 1
});

以下JSON

{ "EURUSD":{"dir":1,"ask":"1.13679","bid":"1.13674"}, "USDJPY":{"dir":1,"ask":"118.995","bid":"118.990"}, "USDCHF":{"dir":1,"ask":"0.94957","bid":"0.94940"}, "GBPUSD":{"dir":1,"ask":"1.54205","bid":"1.54195"}, "AUDUSD":{"dir":1,"ask":"0.77893","bid":"0.77887"}, "NZDUSD":{"dir":1,"ask":"0.75185","bid":"0.75175"}, "GBPJPY":{"dir":1,"ask":"183.496","bid":"183.483"}, "EURGBP":{"dir":1,"ask":"0.73724","bid":"0.73716"} }