0

我正在学习如何解析 json 和查询,并且正在研究其他问题:我看到有人使用下面的 URL 来获取股票代码和值。我也想得到实际的股票价值,但我稍后会弄清楚。

我的 jquery 代码应该解析它提供的 JSON 格式,但我是新手,它似乎并没有按照我理解的方式工作。对不起,如果这是一个“nooby”问题。

http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback

它返回这个,我重新格式化并验证它以使其可读并检查它:

YAHOO.Finance.SymbolSuggest.ssCallback({
   "ResultSet":{
      "Query":"google",
      "Result":[
         {
            "symbol":"GOOG",
            "name":"Google Inc.",
            "exch":"NMS",
            "type":"S",
            "exchDisp":"NASDAQ",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GOOG.MX",
            "name":"Google Inc.",
            "exch":"MEX",
            "type":"S",
            "exchDisp":"Mexico",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GGQ1.DE",
            "name":"GOOGLE-A",
            "exch":"GER",
            "type":"S",
            "exchDisp":"XETRA",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GGQ1.SG",
            "name":"GOOGLE-A",
            "exch":"STU",
            "type":"S",
            "exchDisp":"Stuttgart",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GGQ1.HA",
            "name":"GOOGLE-A",
            "exch":"HAN",
            "type":"S",
            "exchDisp":"Hanover",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GGQ1.MU",
            "name":"GOOGLE-A",
            "exch":"MUN",
            "type":"S",
            "exchDisp":"Munich",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GGQ1.F",
            "name":"GOOGLE-A",
            "exch":"FRA",
            "type":"S",
            "exchDisp":"Frankfurt",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GOOG11BF.SA",
            "name":"GOOGLE      -DRN     MB",
            "exch":"SAO",
            "type":"S",
            "exchDisp":"Sao Paolo",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GOOF.EX",
            "name":"GOOGLE-A",
            "exch":"EUX",
            "type":"S",
            "exchDisp":"EUREX Futures and Options Exchange ",
            "typeDisp":"Equity"
         },
         {
            "symbol":"GGQ1.HM",
            "name":"GOOGLE-A",
            "exch":"HAM",
            "type":"S",
            "exchDisp":"Hamburg",
            "typeDisp":"Equity"
         }
      ]
   }
})

这是我的代码的一部分,用于准确解析该 url:

function(data) {
                  $("#quotes").empty();
                  $.each(data.query.search, function(i, Result){
                    $("#quotes").append("<div>" + ResultSet.Result.symbol + "</a><br>" + ResultSet.Result.name + "<br><br></div>");
                  });
                });
4

1 回答 1

0

根据您尝试使用上面的代码和 JSON 结构实现的目标,您希望遍历每个Result对象并检索该对象的属性,然后将它们作为 div 输出,您可以:

$.each(data.ResultSet.Result, function(i, Result){  
  $("#quotes").append("<div>Symbol: "+Result.symbol+", Name: "+Result.name+" <br/><br/></div>");
});

这是工作的jsFiddle

于 2012-07-08T22:58:18.573 回答