2014-03-29 52 views
0

在我的Titanium天氣項目中,我想解析json數據並在我的項目中顯示數據,但我無法做到。我很努力但仍然失敗。如何顯示json數據

這是我的代碼,是什麼問題?請有人幫助我。

var win = Titanium.UI.createWindow({ 
    backgorundColor: '#000', 
    title: 'My Weather App' 
}); 

var tableview = Ti.UI.createTableView(); 
var data = []; 

var xhr = Ti.Network.createHTTPClient({ 
    onload: function() { 

     alert("success!"); 
     var json = JSON.parse(this.responseText); 
     for (var i = 0; i < json.observation_location.length; i++) { 

      //data.push({"country":json.observation_location[i].country,"city":json.observation_location[i].city}); 
      var row = Ti.UI.createTableViewRow({ 
       height: 60, 
       //filter:observation_location[i]. 
      }); 
      var countryLabel = Ti.UI.createLabel({ 
       text: json.observation_location[i].country, 
       height: 'auto', 
       left: 10, 
       top: 5, 
      }); 
      var cityLabel = Ti.UI.createLabel({ 
       text: json.observation_location[i].city, 
       height: 'auto', 
       left: 15, 

      }); 

      row.add(countryLabel); 
      row.add(cityLabel); 
      data.push(row); 
     } 

     tableview.setData(data); 

    }, 
    onerror: function() { 
     alert('There was an error retrieving the remote data. Try again.'); 
    } 
    //timeout:5000 
}); 

xhr.open("GET", "http://api.wunderground.com/api/02e5dd8c34e3e657/geolookup/conditions/forecast/q/Dhaka,Bangladesh.json"); 
xhr.send(); 

win.add(tableview); 
win.open(); 

回答

0

的JSON返回不顯示爲數據的數組。您可以通過在調試中運行應用程序並在分配json變量之後立即設置斷點來檢查這一點。然後您可以導航返回的數據。雖然返回了很多信息,但沒有一個特別看起來像一個數組,所以我刪除了你的循環。另外,observation_location位於current_observation內部,因此您可以通過json.observation_location.current_observation來訪問它。

var win = Titanium.UI.createWindow({ 
    backgorundColor: '#000', 
    title: 'My Weather App' 
}); 

var tableview = Ti.UI.createTableView(); 
var data = []; 

var xhr = Ti.Network.createHTTPClient({ 
    onload: function() { 

     alert("success!"); 
     var json = JSON.parse(this.responseText); 
     //for (var i = 0; i < json.observation_location.length; i++) { 

      //data.push({"country":json.observation_location[i].country,"city":json.observation_location[i].city}); 
      var row = Ti.UI.createTableViewRow({ 
       height: 60, 
       //filter:observation_location[i]. 
      }); 
      var countryLabel = Ti.UI.createLabel({ 
       text: json.current_observation.observation_location.country, //json.observation_location[i].country, 
       height: 'auto', 
       left: 10, 
       top: 5, 
      }); 
      var cityLabel = Ti.UI.createLabel({ 
       text: json.current_observation.observation_location.city, //json.observation_location[i].city, 
       height: 'auto', 
       left: 15, 

      }); 

      row.add(countryLabel); 
      row.add(cityLabel); 
      data.push(row); 
     //} 

     tableview.setData(data); 

    }, 
    onerror: function() { 
     alert('There was an error retrieving the remote data. Try again.'); 
    } 
    //timeout:5000 
}); 

xhr.open("GET", "http://api.wunderground.com/api/02e5dd8c34e3e657/geolookup/conditions/forecast/q/Dhaka,Bangladesh.json"); 
xhr.send(); 

win.add(tableview); 
win.open(); 
+0

謝謝,我完成了它。 – user3476498