我是Jquery和JSON的新手。我试图使用以下示例代码向api发出请求:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
$.ajax({
"url":"https://www.kimonolabs.com/api{key}",
"crossDomain":true,
"dataType":"jsonp",
//Make a call to the Kimono API following the "url"
'success': function(response){
// If the call request was successful and the data was retrieved, this
function will create a list displaying the data
$(".panel-heading").html(response.name);
//Puts the API name into the panel heading
var collection = response.results.collection1;
for (var i = 0; i < collection.length; i++){
// Traverses through every element in the entire collection
$(".list-group").append('<li class="list-group-item">' +
collection[i].property1.text + '</li>');
// adds the text and the links from the first property into the
list
}
}
});
我的api数据结构是JSON,如下所示:
{
"name": "Kimono tutorial",
"count": 5,
"frequency": "Daily",
"version": 1,
"newdata": false,
"lastrunstatus": "success",
"thisversionstatus": "success",
"nextrun": "Sat Apr 18 2015 14:14:44 GMT+0000 (UTC)",
"thisversionrun": "Wed Apr 15 2015 14:13:33 GMT+0000 (UTC)",
"results": {
"Blog": [
{
"Author": {
"href": "https://www.kimonolabs.com/sandbox.html?author=john-perry",
"text": "John Perry"
},
"Body": "This is the coolest article in the world and there is some stuff that we cannot do in the time to be the place that is not this place to be that in the place to be.",
"Headlines": {
"href": "https://www.kimonolabs.com/sandbox.html?article=1",
"text": "Did you see the Kimono API I made? It's crazy"
}
},
{
"Author": {
"href": "https://www.kimonolabs.com/sandbox.html?author=david-moralis",
"text": "David Moralis"
},
"Body": "This is the coolest article in the world and there is some stuff that we cannot do in the time to be the place that is not this place to be that in the place to be.",
"Headlines": {
"href": "https://www.kimonolabs.com/sandbox.html?article=2",
"text": "Christmas comes late"
}
},
{
"Author": {
"href": "https://www.kimonolabs.com/sandbox.html?author=sarah-perry",
"text": "Sarah Perry"
},
"Body": "This is the coolest article in the world and there is some stuff that we cannot do in the time to be the place that is not this place to be that in the place to be.",
"Headlines": {
"href": "https://www.kimonolabs.com/sandbox.html?article=3",
"text": "Out the window it looks blue"
}
},
{
"Author": {
"href": "https://www.kimonolabs.com/sandbox.html?author=tom-collins",
"text": "Tom Collins"
},
"Body": "This is the coolest article in the world and there is some stuff that we cannot do in the time to be the place that is not this place to be that in the place to be.",
"Headlines": {
"href": "https://www.kimonolabs.com/sandbox.html?article=4",
"text": "These are not the droids you're looking for"
}
},
{
"Author": {
"href": "https://www.kimonolabs.com/sandbox.html?author=marie-marley",
"text": "Marie Marley"
},
"Body": "This is the coolest article in the world and there is some stuff that we cannot do in the time to be the place that is not this place to be that in the place to be.",
"Headlines": {
"href": "https://www.kimonolabs.com/sandbox.html?article=5",
"text": "It isn't what it seems"
}
}
]
}
}
我在页面上有适当的HTML信息。问题是,我知道我需要在这里替换我的集合名称和我的属性:
response.results.collection1
,例如:
collection[i].property1.text
但我不知道要替换哪些!任何帮助,将不胜感激。感谢。