0

我正在处理简单的任务,从 JSON 格式的 url 读取数据并在表中显示这些字段

我写的代码就像

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();

我在 Titanium 上运行它——显示 JSON 数据的第一个警报。之后它没有得到第二个不确定的警报......为什么它不进入下一步......请帮助我解决代码中的任何错误或解析问题。

谢谢德文达

4

1 回答 1

2

您提供的 url 返回的“JSON” ( http://gomashup.com/json.php?fds=geo/usa/zipcode/city/STERLING) 无效。(http://jsonlint.com是检查这类事情的有用资源。)它以 a 开头并以 a(结尾),如下所示:

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

(我已经省略了很多。)

JSON 文档[以or开头,以对应的or{结尾。]}

(如果没有开头和结尾的,上述内容将是有效的),因此您始终可以在解析之前删除它们,但真正的答案是修复提要。

于 2013-03-09T18:57:45.053 回答