如何使用jquery显示json数组?
我试图使用$.getJSON遍历TwitterAPI示例代码中的json数组,并使用jquery显示每个值?但它没有显示任何示例/帮助 我想在谷歌地图中显示每个推特用户的long&lat 提前谢谢 此json数组来自如何使用jquery显示json数组?,jquery,json,Jquery,Json,我试图使用$.getJSON遍历TwitterAPI示例代码中的json数组,并使用jquery显示每个值?但它没有显示任何示例/帮助 我想在谷歌地图中显示每个推特用户的long&lat 提前谢谢 此json数组来自 { “结果”:{ “地点”:[ { “名称”:“推特总部”, “国家”:“美利坚合众国”, “国家代码”:“美国”, “属性”:{ “街道地址”:“Folsom街795号” }, “url”:”http://api.twitter.com/1/geo/id/247f43d441de
{
“结果”:{
“地点”:[
{
“名称”:“推特总部”,
“国家”:“美利坚合众国”,
“国家代码”:“美国”,
“属性”:{
“街道地址”:“Folsom街795号”
},
“url”:”http://api.twitter.com/1/geo/id/247f43d441defc03.json",
“id”:“247f43d441defc03”,
“边界框”:{
“坐标”:[
[
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
]
]
],
“类型”:“多边形”
},
“包含在其中”:[
{
“名称”:“旧金山”,
“国家”:“美利坚合众国”,
“国家代码”:“美国”,
“属性”:{
},
“url”:”http://api.twitter.com/1/geo/id/5a110d312052166f.json",
“id”:“5a110d312052166f”,
“边界框”:{
“坐标”:[
[
[
-122.51368188,
37.70813196
],
[
-122.35845384,
37.70813196
],
[
-122.35845384,
37.83245301
],
[
-122.51368188,
37.83245301
]
]
],
“类型”:“多边形”
},
“全名”:“加利福尼亚州旧金山”,
“地点类型”:“城市”
}
],
“全名”:“旧金山推特总部”,
“地点类型”:“poi”
}
]
},
“查询”:{
“url”:”http://api.twitter.com/1/geo/search.json?query=Twitter+HQ&精度=0&自动完成=错误&粒度=邻域“,
“类型”:“搜索”,
“参数”:{
“粒度”:“邻域”,
“准确度”:0,
“自动完成”:错误,
“查询”:“推特总部”
}
}
}
jQuery TwitterAPI json
$(文档).ready(函数(){
$(“#字母b a”)。单击(函数(){
$.getJSON('geo.json',函数(数据){
$('#tweetstuff').empty();
$.each(data.results,函数(entryIndex,条目){
var html='';
html+='places'+entry.places+'';
html+=“name”+entry.places.name+”;
html+=“country”+entry.places.country+”;
html+='attributes'+entry.places.attributes+'';
html+='url'+entry.places.url+'';
html+='id'+entry.places.id+'';
html+='places'+entry.places.bounding_box+'';
html+='coordinates'+entry.places.coordinates+'';
html+='';
$('#dictionary').append(html);
});
});
返回false;
});
});
jquerytwitterapi示例
json
您的代码中存在大量错误。它是“result”,而不是“results”,它是一个对象,而不是数组,因此您可以使用data.result.places
——这就是好的JavaScript编辑器的帮助所在。此外,您的页面上没有id为“dictionary”的元素。它是
,而不是
以下是工作代码:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery-TwitterAPI json</title>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
var data = {
"result": {
"places": [
{
"name": "Twitter HQ",
"country": "The United States of America",
"country_code": "US",
"attributes": {
"street_address": "795 Folsom St"
},
"url": "http://api.twitter.com/1/geo/id/247f43d441defc03.json",
"id": "247f43d441defc03",
"bounding_box": {
"coordinates": [
[
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
]
]
],
"type": "Polygon"
},
"contained_within": [
{
"name": "San Francisco",
"country": "The United States of America",
"country_code": "US",
"attributes": {
},
"url": "http://api.twitter.com/1/geo/id/5a110d312052166f.json",
"id": "5a110d312052166f",
"bounding_box": {
"coordinates": [
[
[
-122.51368188,
37.70813196
],
[
-122.35845384,
37.70813196
],
[
-122.35845384,
37.83245301
],
[
-122.51368188,
37.83245301
]
]
],
"type": "Polygon"
},
"full_name": "San Francisco, CA",
"place_type": "city"
}
],
"full_name": "Twitter HQ, San Francisco",
"place_type": "poi"
}
]
},
"query": {
"url": "http://api.twitter.com/1/geo/search.json?query=Twitter+HQ&accuracy=0&autocomplete=false&granularity=neighborhood",
"type": "search",
"params": {
"granularity": "neighborhood",
"accuracy": 0,
"autocomplete": false,
"query": "Twitter HQ"
}
}
};
$(document).ready(function() {
$('#letterA A').click(function() {
//$.getJSON('geo.json', function() {
//$('#tweetstuff').empty();
$.each(data.result.places, function(entryIndex, entry) {
var html = '<div class="entry">';
html += 'place ' + entryIndex + '<br/>';
html += 'name: ' + entry.name + '<br/>';
html += 'country: ' + entry.country + '<br/>';
html += 'attributes: ' + entry.attributes + '<br/>';
html += 'url: ' + entry.url + '<br/>';
html += 'id: ' + entry.id + '<br/>';
html += 'places: ' + entry.bounding_box.coordinates + '<br/>';
//html += 'coordinates' + entry.coordinates + '<br/>';
html += '</div>';
$('#dictionary').append($(html));
});
//});
return false;
});
});
</script>
</head>
<body>
<h1>jQuery-Twitter API Example</h1>
json
<div class="letters">
<div class="letter" id="letterA">
<h3><a href="#">A</a></h3>
</div>
</div>
<div id="dictionary">
</div>
</body>
</html>
jQuery TwitterAPI json
风险值数据={
“结果”:{
“地点”:[
{
“名称”:“推特总部”,
“国家”:“美利坚合众国”,
“国家代码”:“美国”,
“属性”:{
“街道地址”:“Folsom街795号”
},
“url”:”http://api.twitter.com/1/geo/id/247f43d441defc03.json",
“id”:“247f43d441defc03”,
“边界框”:{
“坐标”:[
[
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
]
]
],
“类型”:“多边形”
},
“包含在其中”:[
{
“名称”:“旧金山”,
“国家”:“美利坚合众国”,
“国家代码”:“美国”,
“属性”:{
},
“url”:”http://api.twitter.com/1/geo/id/5a110d312052166f.json",
“id”:“5a110d312052166f”,
“边界框”:{
“坐标”:[
[
[
-122.51368188,
37.70813196
],
[
-122.35845384,
37.70813196
],
[
-122.35845384,
37.83245301
],
[
-122.51368188,
37.83245301
]
]
],
“类型”:“多边形”
},
“全名”:“加利福尼亚州旧金山”,
“地点类型”:“城市”
}
],
“全名”:“旧金山推特总部”,
“地点类型”:“poi”
}
]
},
“奎
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery-TwitterAPI json</title>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
var data = {
"result": {
"places": [
{
"name": "Twitter HQ",
"country": "The United States of America",
"country_code": "US",
"attributes": {
"street_address": "795 Folsom St"
},
"url": "http://api.twitter.com/1/geo/id/247f43d441defc03.json",
"id": "247f43d441defc03",
"bounding_box": {
"coordinates": [
[
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
],
[
-122.400612831116,
37.7821120598956
]
]
],
"type": "Polygon"
},
"contained_within": [
{
"name": "San Francisco",
"country": "The United States of America",
"country_code": "US",
"attributes": {
},
"url": "http://api.twitter.com/1/geo/id/5a110d312052166f.json",
"id": "5a110d312052166f",
"bounding_box": {
"coordinates": [
[
[
-122.51368188,
37.70813196
],
[
-122.35845384,
37.70813196
],
[
-122.35845384,
37.83245301
],
[
-122.51368188,
37.83245301
]
]
],
"type": "Polygon"
},
"full_name": "San Francisco, CA",
"place_type": "city"
}
],
"full_name": "Twitter HQ, San Francisco",
"place_type": "poi"
}
]
},
"query": {
"url": "http://api.twitter.com/1/geo/search.json?query=Twitter+HQ&accuracy=0&autocomplete=false&granularity=neighborhood",
"type": "search",
"params": {
"granularity": "neighborhood",
"accuracy": 0,
"autocomplete": false,
"query": "Twitter HQ"
}
}
};
$(document).ready(function() {
$('#letterA A').click(function() {
//$.getJSON('geo.json', function() {
//$('#tweetstuff').empty();
$.each(data.result.places, function(entryIndex, entry) {
var html = '<div class="entry">';
html += 'place ' + entryIndex + '<br/>';
html += 'name: ' + entry.name + '<br/>';
html += 'country: ' + entry.country + '<br/>';
html += 'attributes: ' + entry.attributes + '<br/>';
html += 'url: ' + entry.url + '<br/>';
html += 'id: ' + entry.id + '<br/>';
html += 'places: ' + entry.bounding_box.coordinates + '<br/>';
//html += 'coordinates' + entry.coordinates + '<br/>';
html += '</div>';
$('#dictionary').append($(html));
});
//});
return false;
});
});
</script>
</head>
<body>
<h1>jQuery-Twitter API Example</h1>
json
<div class="letters">
<div class="letter" id="letterA">
<h3><a href="#">A</a></h3>
</div>
</div>
<div id="dictionary">
</div>
</body>
</html>