I'm having a hard time converting my code to read JSON from a file for a tableview versus having it defined on the page.
My desired end result is to convert this, which works....
var tabledata = [ {title:'Company 1', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 2', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 3', category:['Designer Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 4', category:['Eco Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, ]; var tableview = Titanium.UI.createTableView({ data:tabledata, style:1 });To something like this, which doesnt...
Ti.include("/etc/readJSON.js"); var tabledata = readJSON('exhibitors.JSON')I have a JSON file in my applicationDataDirectory with these contents.
[{"title":"Company 1", "category":{"Contract Fabrics"}, "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}, {"title":"Company 2", "category":{"Contract Fabrics"}, "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}, {"title":"Company 3", "category":{"Designer Friendly"}, "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}, {"title":"Company 4", "category":{"Eco Friendly"}, "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}]I have also tried it like this, which also doesn't parse properly.
[ {title:'Company 1', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 2', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 3', category:['Designer Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}, {title:'Company 4', category:['Eco Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'}]I have this function to read in the JSON, which fails on "data = JSON.parse(dataSrc);"
function readJSON(filename) { var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); if (file.exists()) { var dataSrc = Ti.Platform.osname==='android'? ''+file.read():file.read(); data = JSON.parse(dataSrc); } return data; }I get this error...
[WARN] Exception in event callback. { line = 7; message = "Unable to parse JSON string"; name = SyntaxError; sourceId = 200029856; sourceURL = "file://localhost/Users/djonesax/Library/Application%20Support/iPhone%20Simulator/4.3/Applications/E332FC8D-C00C-4BF8-8178-DD5B211D93A8/mobileapp.app/etc/readJSON.js"; }Any help would be appreciated. Can this even be done this way?
2 Answers
Accepted Answer
In your first example, your JSON is not valid. You have to write valid json in order to parse correctly. The following works fine.
app.js:
var win = Ti.UI.createWindow({ backgroundColor:'#fff' }); var file = Ti.Filesystem.getFile('json.txt'); var data = file.read().text; var json = JSON.parse(data); var tv = Ti.UI.createTableView({ data:json }); win.add(tv); win.open();json.txt:
[{"title":"Company 1", "category":["Contract Fabrics"], "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}, {"title":"Company 2", "category":["Contract Fabrics"], "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}, {"title":"Company 3", "category":["Designer Friendly"], "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}, {"title":"Company 4", "category":["Eco Friendly"], "hasChild":"true", "selectedBackgroundColor":"orange", "test":"ui/common/showtime/exhibitor"}]
hello, file.read() returns a Titanium.Blob not a string that can be parsed, try file.read().text instead
Your Answer
Think you can help? Login to answer this question!