【问题标题】:How to call values from a JSON API如何从 JSON API 调用值
【发布时间】:2016-02-23 10:42:23
【问题描述】:

我有一个这样的 JSON 文件,保存为 runs.json,我想使用 ajax 调用 json 文件,但我真正想要的 json 值没有反复显示,我没有定义

{
    "status": "true",
    "Content-Range": {
        "AthletesRange": [
            {
                "first": "1",
                "last": "1",
                "max": "1"
            }
        ]
    },
    "data": {
        "Athletes": [
            {
                "AthleteID": "600",
                "FirstName": "Edward",
                "LastName": "HAWTHORNE",
                "HomeRunID": "1",
                "Sex": "M",
                "CountryCode": "97",
                "Avatar": "images.parkrun.com/app/general/parkrun_default_avatar_200x200.png"
            }
        ]
    },
    "links": [
        {
            "rel": "self",
            "href": "\"/v1/athletes/600?offset=0&limit=100\""
        }
    ],
    "timestamp": 1456223662,
    "originalQryTime": 1456223662
}

My AJAX call is like this

<script>

    $(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "runs.json",
        dataType: 'json',
       success: function(result){
        var first = result.FirstName;
        var last = result.LastName;
        $('#runner-name').append(' Welcome ' + first +  last );
    }
            });
    });
</script>

<body>
 <div>
     <h1 id="runner-name"></h1>
 </div>
</body>

But am continually getting undefined as the value passed out, I would like if anyone could help me with this JSON call because i need the FirstName and LastName of the particular Athlete.

【问题讨论】:

  • result.data.Athletes[0].FirstName

标签: jquery json ajax api


【解决方案1】:
 var first = result.data.Athletes[0].FirstName;
 var last = result.data.Athletes[0].LastName;

这应该适合你

【讨论】:

    【解决方案2】:

    你必须把它分解成一个嵌套的方法。

    data
         Athletes
                 FirstName
    

    因此:

    var returnedResult = result.data.Athletes[i].FirstName;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-21
      • 2021-11-22
      • 2020-06-05
      • 2012-10-31
      • 2018-10-24
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      相关资源
      最近更新 更多