晚上好,
我在使用JSON时遇到了一个简单的问题。我一个深化发展的SmartWatch(卵石)应用程序,它从API获取数据,其返回如下:
使用javascript访问最后一个JSON元素
{
"name": "toto",
"count": 55,
"results": {
"collection1": [
{
"article": "Content number 1"
},
{
"article": "Content number 2"
},
{
"article": "Content number 3"
}
]
}
}
的问题是:是否有可能只访问从这个最新的“文章”的内容JSON?所以在这种情况下,它将是“内容编号3”。
这里是我这么远,但只返回第1个要素:
ajax({ url: 'MY_API_URL', type:'json' }, function(data){
simply.vibe(); // Vibrate the watch
var message = data.results.collection1[0].article;
simply.body(message); // display the message on the watch
非常感谢!
和[选择最后一个元素中的JavaScript Array(http://stackoverflow.com/q/9050345/218196),通过[谷歌](HTTPS都发现:// www.google.com/search?q=javascript%20array%20get%20last%20element)。 –