How to display JSON result from webservice in TableView
Hey Guys & Girls
I hope some of you out there can point me in the right direction. I haven't been working with JSON so much and now I have to take the results from an external web service and present that as a TableView in Titanium. Any hints on how to parse the JSON result and get the contents?
Exerpt from JSON result below:
{
"POHeader" :
[
{
"POEstDeliveryDate" : "2008-01-08T00:00:00CET",
"PONumber" : 1000,
"POStatus" : "Slutlev, fakt.avpr, konterad",
"SupplierId" : "10000",
"POValue" : 0.0000
},
{
"POEstDeliveryDate" : "2008-01-15T00:00:00CET",
"PONumber" : 1009,
"POStatus" : "Slutlev, fakt.avpr, konterad",
"SupplierId" : "10000",
"POValue" : 0.0000
}
]
}
1 Answer
-
You want to do something like this:
Ps. this code has not been tested and written from the to of my head.
var myTable = Ti.UI.createTableView(); var tableData = []; var client = Ti.Network.createHTTPClient(); client.open('GET', 'someUrlHere'); client.onreadystatechange = function(e) { if(this.readystate == 4) { var json = JSON.parse(this.responseData); for(var i=0; i < json.POHeader.length;i++) { var header = json.POHeader[i]; var row = Ti.UI.createTableViewRow({ title:header.PONumber }); // add anything more to the row, like other labels etc. tableData.push(row); } myTable.setData(tableData); } }; client.send();