从bandsintown API解析json

时间:2012-05-17 20:50:14

标签: json api titanium-mobile appcelerator-mobile

我无法在appcelerator移动应用中解析并显示来自Bands in Town的API中的事件。 (IOS) 这是我要在表格中显示的乐队活动。 http://api.bandsintown.com/artists/Lucy%20Seven/events.json?api_version=2.0&app_id=LucySeven 这是我展示它的代码

var win = Ti.UI.currentWindow;

win.hideNavBar();

Ti.UI.backgroundColor = '#050505';

var url = "http://api.bandsintown.com/artists/Lucy%20Seven/events.json?      api_version=2.0&app_id=LucySeven"

var table = Ti.UI.createTableView({

        backgroundColor: '#050505',
        separatorColor:'#110000',
    });
var tableData = [];
var json, artists, name, picture, title, description;

var xhr = Ti.Network.createHTTPClient({
onload: function() {
// Ti.API.debug(this.responseText);

json = JSON.parse(this.responseText);
for (i = 0; i < json.data.length; i++) {
    data = json.data[i];
    row = Ti.UI.createTableViewRow({
        height:'100dp',
        backgroundColor: '#050505',
        separatorColor:'#110000',
    });
  var  name = Ti.UI.createLabel({
        text: title,
        font:{
            fontSize:'17dp',
        fontWeight:'bold'
    },
    height:'auto',
    left:'90dp',
    top:'20dp',
    color:'#eee',
    touchEnabled:true
    });
    row.add(name);


        var  start = Ti.UI.createLabel({
        text:   description,
        font:{
            fontSize:'12dp'
        },
    height:'auto',
    left:'90dp',
    bottom:'20dp',
    color:'#eee',
    touchEnabled:true
    });
    row.add(start);


  // Avatar
            var img = Ti.UI.createImageView({
                image   : thumb_url ,
                width   : 70,
                height  : 70,
                top     : 5,
                bottom  : 5,
                borderRadius: 5,
                borderColor: '#eee',
                left    : 5
            });
            row.add(img);


    tableData.push(row);
    }

table.setData(tableData);
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT:   " + this.responseText);
Ti.API.debug("ERROR:  " + e.error);
alert('There was an error retrieving the remote data. Try again.');
},
timeout:5000
});

xhr.open("GET", url);
xhr.send();

这里有一个json的API响应: http://www.bandsintown.com/api/responses#events-json 我真的看不出有什么问题......也许我会盲目地看到我错过了什么? 如果有人能指出我正确的方向,我将不胜感激。 我试过:data.title data.artists.title title artists.titel等等,但我的tableview中没有显示..... 感谢名单 // - [R

1 个答案:

答案 0 :(得分:1)

this.responseText的价值是什么?json之后JSON.parse的价值是多少?在JSON响应中,我没有看到data属性,因此我不确定json.data应该是什么。同样在Ti.UI.createLabel中,您test: titletitle永远不会给出值。

我怀疑你for循环中你真正想要的是:

json = JSON.parse( this.responseText ); // `json` will be an array of objects

for (i = 0; i < json.length; i++) {
  data = json[ i ];
  // ...

  var name = Ti.UI.createLabel( {
    text: data.title,
    // ...
  } );
}

调试它的关键与调试很多东西相同 - 找出每一步你有什么数据(我从来没有使用过Titanium,但它至少必须有类似console.log的东西)和数字它与你的期望有何不同。