[英]How to call values from a JSON API
我有一个像这样的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.
var first = result.data.Athletes[0].FirstName;
var last = result.data.Athletes[0].LastName;
这应该为你工作
您必须将其分解为嵌套式方法。
data
Athletes
FirstName
因此:
var returnedResult = result.data.Athletes[i].FirstName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.