2014-03-29 3 views
0

티타늄 날씨 프로젝트에서 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

관련 문제