我需要读取一个在线JSON数据库(这是链接)
{
"GOLD": {
"symbol": "GOLD",
"lasttime": 1378862415,
"digits": 2,
"change": "-0.09",
"bid": "1364.15",
"ask": "1364.75"
},
"SILVER": {
"symbol": "SILVER",
"lasttime": 1378862413,
"digits": 3,
"change": "-0.005",
"bid": "22.945",
"ask": "22.985"
}
}
我需要通过jQuery将黄金和白银值带到我的HTML页面,我写了这个,但它不起作用,但HTML中没有出现任何内容。
$.getJSON('https://quotes.instaforex.com/get_quotes.php?m=json&q=gold,silver', function (data) {
var items = [];
$.each(data, function (key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
如果我使用此 JSON,使用相同的代码,它可以工作:
{
"symbol": "GOLD",
"lasttime": 1378862415,
"digits": 2,
"change": "-0.09",
"bid": "1364.15",
"ask": "1364.75"
}
我如何告诉脚本读取正确的 JSON,同时使用:金色和银色,您可以在首页链接中找到的那个?
我想做这样的东西。
这里不需要file_get_contents(),因为他说"如果我使用此JSON,则使用相同的代码,它可以工作:"
MIOII,你已经回答了你的问题。
{
"symbol": "GOLD",
"lasttime": 1378862415,
"digits": 2,
"change": "-0.09",
"bid": "1364.15",
"ask": "1364.75"
}
是更大事物的一部分...
{
"GOLD": {
"symbol": "GOLD",
"lasttime": 1378862415,
"digits": 2,
"change": "-0.09",
"bid": "1364.15",
"ask": "1364.75"
},
"SILVER": {
"symbol": "SILVER",
"lasttime": 1378862413,
"digits": 3,
"change": "-0.005",
"bid": "22.945",
"ask": "22.985"
}
}
将其视为多维数组:
JSON[
GOLD[symbol,lasttime...]
SILVER[]
sthelse[]
]
你的答案:再循环JSON,再循环它的子项
$.getJSON('https://quotes.instaforex.com/get_quotes.php?m=json&q=gold,silver', function(JSON) {
var items = [];
$.each(JSON, function(JSONchildren, contentsOfJSONchildren) {
//JSONchildren - GOLD, SILVER
$.each(contentsOfJSONchildren, function(index, value) {
//index - symbol,lasttime,digits...
//value - value of index
items.push('<li id="' + index + '">' + value + '</li>');
});
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
现在格式
items.push('<li id="' + index + '">' + value + '</li>');
如您所愿
// segnalato dove andare a cercare ora definisco gli oggetti
$.getJSON('JSON-DOCUMENT.php', function(json) {
var N°1 = json.VAR-N°1;
var N°2 = json.VAR-N°2;
...ECC...
var N°1 = SOME OPERATIONS IF YOU WANT
var N°2 = SOME OPERATIONS IF YOU WANT
// pubblico in DIV tramite funzione .text
$("YOUR-DIV").text(+ goldAsk');
});