簡體   English   中英

使用Appcelerator無法獲取JSON文件以將其解析為JavaScript中的TableView

[英]Can't get JSON file to parse into TableView in JavaScript using Appcelerator

我正在嘗試使用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"}

    }

您需要將所有依賴於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();

Jodo,您確實需要閱讀有關在TableView上使用HTTP ClientSet Data的文檔。

Titanium使用遠程數據的文檔中有一個非常干凈的示例

您使用的url除了返回該網站的HTML輸出外沒有返回其他任何內容,其次,您不能簡單地在tableview上設置data屬性,除非以這種方式之一格式化數據變量。

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

而且,TableView的data屬性采用行/部分數組或格式良好的字典。 看到更多的TableView指南

如果您以前從未嘗試過任何操作,建議您先閱讀文檔。 它將幫助您更好地學習和理解,並且肯定會節省您的寶貴時間。 :) 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM