功能(索引,值不呈现数据?

时间:2018-02-13 19:56:44

标签: javascript json

<html>
<head><title>Test</title></head>
<script src="jquery.js"></script>
<body>
    <script type="text/javascript">
        $.ajax({
            url: 'https://www.alphavantage.co/query?function=SMA&symbol=MSFT&interval=weekly&time_period=10&series_type=open&apikey=_______',
            dataType: 'json',
            type: 'get',
            cache: false,
            success: function(data){
                $(data.SMA).each(function(index, value){
                    console.log(value);
                });
            }
        });
    </script>
</body>
<html>

数据

enter image description here

我试图让数据显示价格,但是当我提供索引时,它不会显示数据中显示的价格。

它没有显示任何内容,我缺少什么?

1 个答案:

答案 0 :(得分:0)

SMS值位于此对象Technical Analysis: SMA

console.log('Retrieving data...');
$.ajax({
  url: 'https://www.alphavantage.co/query?function=SMA&symbol=MSFT&interval=weekly&time_period=10&series_type=open&apikey=G8M336NY2UCXEZL7',
  dataType: 'json',
  type: 'get',
  cache: false,
  success: function(data) {
    var target = data['Technical Analysis: SMA'];
    $(Object.keys(target)).each(function(index, key) {
      console.log(target[key].SMA);
    });
    
    console.log('Done!');
  }
});
.as-console-wrapper {
  max-height: 100% !important
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>