2013-09-10 79 views
-2

我需要阅读的在线JSON数据库(这里的link如何阅读在线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:黄金和白银,一个你可以在首页链接找到?

我想制作一些类似this的东西。

MIOII,你已经回答了你的问题:因为他说:“在相同的代码,如果我用这个JSON它的作品”在这里不需要

+0

尝试警惕,如果你得到任何 –

+3

你从quotes.instaforex.com运行代码?如果不是,则不可能,跨域呼叫被阻止。您必须找到一个以JSONP格式提供结果的数据库。 – bfavaretto

+1

@bfavaretto或者,您可以在服务器上创建服务器端代理。在PHP中,它只是'echo file_get_contents([URL HERE]);'。然后你用正常的JSON加载你的脚本,并且不需要JSONP。 – Jasper

回答

-1
// 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'); 

}); 
1

的file_get_contents()。

{ 
     "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>'); 

,因为你需要对键和值它