[英]Iterating through YQL JSON results in geo.places via javascript
I'm trying to iterate through some JSON results from the YQL geo.places table using the YQL jQuery plugin ( http://plugins.jquery.com/project/jquery-yql ) troubleshooting with the following code; 我正在尝试使用YQL jQuery插件( http://plugins.jquery.com/project/jquery-yql )使用以下代码对YQL geo.places表中的一些JSON结果进行迭代。
$.yql("select * from geo.places where text=#{placename}", {
placename: '90210'
},
function(data) {
var Results = data.query.results;
$.each(Results.place, function(name, value) {
alert(name + ":" + value);
});
});
});
Except, whenever there is more than one "place" in the results, the alert will spit back "0:[object][object]", "1:[object][object]", etc (for each place). 除此之外,只要结果中有多个“地点”,警报就会吐出“0:[对象] [对象]”,“1:[对象] [对象]”等(对于每个地方)。 Whenever there is only one place result, the alert will spit back all the names and values of just the one place (woeid, name, country, admin1, etc.) 只要一个地方结果,警报就会吐出一个地方的所有名称和价值(woeid,name,country,admin1等)
Essentially I'd like to... 基本上我想......
input a zip code to YQL 输入邮政编码到YQL
verify the city/state/country from results against user submitted data 根据用户提交的数据从结果中验证城市/州/国家/地区
update latitude and longitude fields from the results 从结果中更新纬度和经度字段
Thanks! 谢谢!
If Yahoo returns a single place
, it does so as a property of results
, not as an array of places. 如果雅虎返回一个place
,它会作为results
的属性,而不是一个地方数组。 If it's not an array, you can't iterate. 如果它不是数组,则无法迭代。 So test whether it's an array; 所以测试它是否是一个数组; if it's not, make it one: 如果不是,那就把它做成一个:
$.yql("select * from geo.places where text=#{placename}", {
placename: '90210'
},
function(data) {
var Places = data.query.results.place;
if (!Places.length) Places = [Places];
$.each(Places, function(index, place) {
alert(index + ":" + place.name);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.