无法使用 Appcelerator 将 JSON 文件解析为 JavaScript 中的 TableView
Can't get JSON file to parse into TableView in JavaScript using Appcelerator
我正在尝试使用 Appcelerator 将 JSON 文件放入 JavaScript 中的 table,我不太确定为什么它输出为空 table 编译到示例页面时。我是处理 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();
url返回的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
块内。否则,table 将在 tableData
有任何数据之前构造。您实际上也没有发送 xhr
.
一旦你得到一个 URL 实际上 returns 那个 JSON,你可以使用这个:
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,您真的需要阅读有关使用 HTTP Client & Set Data on TableView 的文档。
Titanium's Working With Remote Data
的文档中有一个非常干净的示例
您正在使用的 url 除了该网站的 HTML 输出之外没有返回任何其他内容,其次您不能简单地在 tableview 上设置数据 属性 除非您的数据变量以这种方式之一格式化。
[ {title: 'Apples'}, {title: 'Bananas'}, {title: 'Carrots'}, {title: 'Potatoes'} ];
此外,TableView 的数据 属性 采用 Rows/Sections 数组或格式良好的字典。查看更多 TableView Guide
如果您以前从未这样做过,我们建议您在尝试任何操作之前先阅读文档。它将帮助您更好地学习和理解,并且肯定会节省您宝贵的时间。 :) 谢谢
我正在尝试使用 Appcelerator 将 JSON 文件放入 JavaScript 中的 table,我不太确定为什么它输出为空 table 编译到示例页面时。我是处理 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();
url返回的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
块内。否则,table 将在 tableData
有任何数据之前构造。您实际上也没有发送 xhr
.
一旦你得到一个 URL 实际上 returns 那个 JSON,你可以使用这个:
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,您真的需要阅读有关使用 HTTP Client & Set Data on TableView 的文档。
Titanium's Working With Remote Data
的文档中有一个非常干净的示例您正在使用的 url 除了该网站的 HTML 输出之外没有返回任何其他内容,其次您不能简单地在 tableview 上设置数据 属性 除非您的数据变量以这种方式之一格式化。
[ {title: 'Apples'}, {title: 'Bananas'}, {title: 'Carrots'}, {title: 'Potatoes'} ];
此外,TableView 的数据 属性 采用 Rows/Sections 数组或格式良好的字典。查看更多 TableView Guide
如果您以前从未这样做过,我们建议您在尝试任何操作之前先阅读文档。它将帮助您更好地学习和理解,并且肯定会节省您宝贵的时间。 :) 谢谢