繁体   English   中英

无法访问JSON数组中的对象

[英]Trouble with accessing object in JSON array

我似乎无法弄清楚如何使此代码片段正常工作。 我正在尝试访问此json代码段中的“名称”对象。 任何帮助,将不胜感激。

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script> $(document).ready(function () { $.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function(data) { $('#demo').text(data[0].Name); }); }); </script> </head> <body> <p id="demo"></p> </body> </html> 

用这个:

$(document).ready(function () {
    $.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function(data) {
        $('#demo').text(data.query.results.quote.Name);
    });
});
{
    "query": {
        "count": 1,
        "created": "2017-02-13T18:34:48Z",
        "lang": "es-419",
        "results": {
            "quote": {
              "name": "Blabla"

所以你有data.query.results.quote.name

您的API调用不返回数组,而是返回JSON对象。

试试: $('#demo').text(data.query.results.quote.Name);

返回的数据结构如下所示:

{
    "query": {
        "count": 1,
        "created": "2017-02-13T18:34:12Z",
        "lang": "en-us",
        "results": {
            "quote": {
                // other props...
                "Name": "Apple Inc.",
                // other props...
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM