Devendar
Devendar

Reputation: 413

JSON parse issue in Titanium

I am working on simple task read the data from url which is in JSON format and display those fields in Table

i written code like as

var Win = Titanium.UI.currentWindow;  
//SEARCH BAR
var xhr = Titanium.Network.createHTTPClient();

tableData=[];
Win.open();
xhr.onload = function() {
    alert('res'+this.responseData);
     var json = this.responseText;
    var response = JSON.parse(json);
    //-- Mail was sent
    alert('respoinse length : '+response.result.length);
   tableData=[];
    for (i = 0; i < response.result.length; i++) {
            sresult = response.result[i];
            //alert('City'+ sresult.city);


            var row = Ti.UI.createTableViewRow({
                rowID: i,
                color:'#222',


    left:70, top:44,
    width:360,


                text:sresult.County
            });
            tableData.push(row);
        }

      table.setData(tableData);  


};

var table = Titanium.UI.createTableView({

        top:60,
        data:tableData

   });
    Win.add(table);
//xhr.open('GET', 'https://www.clia.livestockid.ca/CLTS/public/division/CCIA/en/splash_logo.jpg');
xhr.open('GET', 'http://gomashup.com/json.php?fds=geo/usa/zipcode/city/STERLING');
xhr.send();

i run it on Titanium - the first alert showing the JSON data. after that its not getting alert of second one not sure ... why its not moving to next step... please help me on this is there any mistakes in code or else parsing issue.

Thanks Devendar

Upvotes: 0

Views: 893

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075855

The "JSON" returned by the url you gave (http://gomashup.com/json.php?fds=geo/usa/zipcode/city/STERLING) is invalid. (http://jsonlint.com is a useful resource for checking that sort of thing.) It starts with a ( and ends with a ), like this:

({
    "result":[
    {
        "Longitude" : "-071.939375",
        "Zipcode" : "01564",
        "ZipClass" : "STANDARD",
        "County" : "WORCESTER",
        "City" : "STERLING",
        "State" : "MA",
        "Latitude" : "+42.366765"
    }
]}
)

(I've omitted a lot from that.)

JSON documents start with either [ or {, and end with the corresponding ] or }.

The above would be valid without the ( at the beginning and ) at the end, so you could always remove them before parsing, but the real answer is to fix the feed.

Upvotes: 2

Related Questions