简体   繁体   English

PebbleJS中的json数据提取

[英]json data extraction in pebbleJS

I am stuck in json data extraction in pebble. 我陷入了卵石的json数据提取中。

var UI = require('ui');
var ajax = require('ajax');

var URL="http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=4a9f5581a9cdf20a699f540ac52a95c9&limit=10&format=json&callback=?";
var card = new UI.Card({
  title:'last.fm stat',
  subtitle:'Fetching...'
});
card.show();

ajax({ url: URL }, function(data){
  var topArtist=data.topartists[0].artist.name;
  card.subtitle(topArtist);
});

Here's the error I get: 这是我得到的错误:

[INFO] ocess_manager.c:368: Heap Usage for App <lastfm sta: Total Size <48584B> Used <6256B> Still allocated <28B>
[PHONE] pebble-app.js:?: (+) [card 1] : [card 1]

[PHONE] pebble-app.js:?: JavaScript Error:
TypeError: Cannot read property '0' of undefined
    at pebble-js-app.js:123:32
    at pebble-js-app.js:871:17
    at req.onreadystatechange (lib/ajax.js:11

4:9) 4:9)

Evening Mona, 晚上蒙娜丽莎

  1. Remove the question mark at the URL's end. 删除网址末尾的问号。

  2. Remove the card.show() instruction where you put it, and place it after adding a subtitle to it. 删除放置它的card.show()指令,并在添加字幕后将其放置。

  3. Specify you're dealing with a JSON datatype. 指定您要处理的是JSON数据类型。

And your final code should now look like this: 现在,您的最终代码应如下所示:

var UI = require('ui');
var ajax = require('ajax');

var URL="http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=4a9f5581a9cdf20a699f540ac52a95c9&limit=10&format=json&callback=";
var card = new UI.Card({
    title:'last.fm stat',
    subtitle:'Fetching...'
});

ajax({ url: URL, type: 'json' }, function(data) {
    var topArtist = data.topartists.artist[0].name;
    card.subtitle(topArtist);
    card.show();
});

It should now run perfectly. 现在应该可以完美运行了。 :) :)

Also, you should add a failure callback in your ajax method: 另外,您应该在ajax方法中添加失败回调:

ajax({object}, success, failure)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM