Aspiring Developer
Aspiring Developer

Reputation: 313

JSON Parsing in Titanium Appcelerator Mobile SDK

I want to parse following JSON using Titanium Appcelerator Mobile SDK

[{"City":null,"Distance":469,"Email":null,"Latitude":0,"Longitude":0,"Name":"Cretin-Derham Hall","PhoneNo":null,"SchoolId":75,"State":null,"Zip":null},{"City":null,"Distance":213,"Email":null,"Latitude":0,"Longitude":0,"Name":"East Ridge","PhoneNo":null,"SchoolId":76,"State":null,"Zip":null},{"City":null,"Distance":26,"Email":null,"Latitude":0,"Longitude":0,"Name":"Hastings","PhoneNo":null,"SchoolId":78,"State":null,"Zip":null},{"City":null,"Distance":116,"Email":null,"Latitude":0,"Longitude":0,"Name":"Stillwater","PhoneNo":null,"SchoolId":81,"State":null,"Zip":null},{"City":null,"Distance":0,"Email":null,"Latitude":0,"Longitude":0,"Name":"White Bear Lake","PhoneNo":null,"SchoolId":82,"State":null,"Zip":null}]

I want to parse this and put all "Name" values in TableView/ListView. Can anyone help me ?

Here's my current code that I have:

   var xhr = Titanium.Network.createHTTPClient();

xhr.onload = function()
{
    Ti.API.info('in utf-8 onload for GET');

    var schools = eval('(' + this.responseText +')');
};

Thanks in advance

Upvotes: 1

Views: 5631

Answers (1)

Rene Pot
Rene Pot

Reputation: 24815

Assuming the responseText contains the JSON and there is a window available in the window variable

var tv = Ti.UI.createTableView();
window.add(tv);

xhr.onload = function()
{
    var data = [];
    Ti.API.info('in utf-8 onload for GET');
    var schools = JSON.parse(this.responseText);
    for (s in schools)
    {
        data.push(Ti.UI.createTableViewRow({title: schools[s].Name});
    }

    tv.data = data;

};

Upvotes: 8

Related Questions