JQuery的解析JSON数组 [英] JQuery Parsing JSON array
问题描述
我有一个 JSON
输出类似如下:
I have a JSON
output like the following:
["City1","City2","City3"]
我希望得到每个城市的名字,我怎么能这样做呢?
I want to get each of the city names, how can i do this?
$.getJSON("url_with_json_here",function(json){
});
编辑:
$.getJSON('url_here', function(data){
$.each(data, function (index, value) {
$('#results').append('<p>'+value+'</p>');
console.log(value);
});
});
以上似乎并不奏效,没有值被输出。
The above doesn't seem to be working, no values are outputted.
推荐答案
的getJSON()
也将解析JSON为你取后,所以从那时起,你是一个简单的JavaScript阵列( []
标记的数组在JSON)。该文件还对如何处理所取得的数据的例子。
getJSON()
will also parse the JSON for you after fetching, so from then on, you are working with a simple Javascript array ([]
marks an array in JSON). The documentation also has examples on how to handle the fetched data.
您可以使用为获得一个数组所有的值
循环:
You can get all the values in an array using a for
loop:
$.getJSON("url_with_json_here", function(data){
for (var i = 0, len = data.length; i < len; i++) {
console.log(data[i]);
}
});
检查您的控制台查看输出( Chrome浏览器,的Firefox/Firebug , IE )。
的jQuery还提供 $。每个()
为迭代,所以你也可以这样做:
jQuery also provides $.each()
for iterations, so you could also do this:
$.getJSON("url_with_json_here", function(data){
$.each(data, function (index, value) {
console.log(value);
});
});
这篇关于JQuery的解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!