无法使用Appcelerator将JSON文件解析为JavaScript中的TableView

时间:2016-05-18 22:57:07

标签: javascript json parsing appcelerator appcelerator-titanium

我正在尝试使用Appcelerator将JSON文件放入JavaScript表格中,我不太确定在编译为示例页面时它为什么输出为空表。我是处理JavaScript和JSON格式的新手,所以如果你看到任何愚蠢的逻辑或语法错误,请放轻松我,并让我知道如何解决我的问题:

// Set the background color with no windows or tabs
Titanium.UI.setBackgroundColor('#000');

// Create the window
var win1 = Titanium.UI.createWindow({  
    title:'Challenge Window',
    backgroundColor:'#fff',
});

// Store the image and its properties
    var image = Titanium.UI.createImageView({
    image: "https://myavantiservices.files.wordpress.com/2015/02/helloworld.gif",
    height: 380,
    width: 380,
    center: 512,
    top: -50
});

var tableData;

// Parse our JSON file using onload
var url = "https://www.sitepoint.com/twitter-json-example/";
var json;
var xhr = Ti.Network.createHTTPClient({
    onload: function() {
        json = JSON.parse(this.responseText);
        tableData = json;
    }
});

// Create the table and insert the JSON data
var table = Titanium.UI.createTableView({
    data: tableData,
    height: 480,
    width: 480,
    top: 256,
    left: 232
});

// Add the image to the window and open the window
win1.add(image);
win1.add(table);
win1.open();

网址:

返回的JSON
    {"results":[

     {"text":"@twitterapi  https://code.google.com/archive/p/twitter-api/issues/353",

     "to_user_id":396524,

     "to_user":"TwitterAPI",

     "from_user":"jkoum",

     "metadata":

     {

      "result_type":"popular",

      "recent_retweets": 109



     },

     "id":1478555574,   

     "from_user_id":1833773,

     "iso_language_code":"nl",

     "source":"twitter< /a>",

     "profile_image_url":"http://s3.amazonaws.com/twitter_production/profile_images/118412707/2522215727_a5f07da155_b_normal.jpg",

     "created_at":"Wed, 08 Apr 2009 19:22:10 +0000"},

     ... truncated ...],

     "since_id":0,

     "max_id":1480307926,

     "refresh_url":"?since_id=1480307926&q=%40twitterapi",

     "results_per_page":15,

     "next_page":"?page=2&max_id=1480307926&q=%40twitterapi",

     "completed_in":0.031704,

     "page":1,

     "query":"%40twitterapi"}

    }

2 个答案:

答案 0 :(得分:0)

您需要将依赖于JSON响应的所有内容移动到onload块内。否则,将在tableData有任何数据之前构建表。您还没有实际发送xhr

获得实际返回该JSON的URL后,您可以使用:

var url = "https://www.sitepoint.com/twitter-json-example/";
var json;
var xhr = Ti.Network.createHTTPClient({
    onload: function() {
        json = JSON.parse(this.responseText);
        tableData = json;

        var table = Titanium.UI.createTableView({
            data: tableData,
            height: 480,
            width: 480,
            top: 256,
            left: 232
        });

        win1.add(image);
        win1.add(table);
        win1.open();
    }
});
xhr.open("GET", url);
xhr.send();

答案 1 :(得分:0)

<)Jodo,你真的需要阅读关于使用HTTP Client&amp;的文档。 Set Data on TableView

Titanium's Working With Remote Data

的文档中有一个非常干净的例子

您正在使用的网址不会返回该网站的HTML输出以外的任何内容,其次您不能简单地在tableview上设置数据属性,除非您的数据变量采用这种方式格式化。

[ {title: 'Apples'}, {title: 'Bananas'}, {title: 'Carrots'}, {title: 'Potatoes'} ];

此外,TableView的data属性采用行/节的数组或格式良好的字典。有关更多TableView Guide

的信息,请参阅此内容

我们建议您在尝试之前先阅读文档,如果您之前没有这样做的话。它会帮助你学习和学习了解更好,绝对会节省您宝贵的时间。 :)谢谢