gpt4 book ai didi

javascript - 使用 JSON 创建数组

转载 作者:行者123 更新时间:2023-12-03 12:16:43 25 4
gpt4 key购买 nike

我正在使用 Google map API 创建 map 。

基本上,调用任何标记的数组结构如下:

var station = [
['SEDRANO DI SAN QUIRINO (PN)',46.048887,12.657924,'Via Maniago, 28','Tel. 0434 918900','sedrano-di-san-quirino.jpg',true,true,false,false,false,false,false],
['CORTINA D\'AMPEZZO (BL)',46.541307,12.132348,'Via dello Stadio, 7','Tel. 0436 869133','cortina-dampezzo.jpg',true,true,false,false,false,true,false],
['POINCICCO DI ZOPPOLA (PN)',45.948725,12.745332,'Via Cusano, 14','Tel. 0434 574831','poinciccio-di-zoppola.jpg',true,true,false,false,false,true,false]
];

现在,我将使用 json 调用创建一个数组,所以...第一步我使用 php 调用标记并将它们放入 json_encode

$sth = mysql_query("SELECT * FROM markers");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);

结果

[{"city":"SEDRANO DI SAN QUIRINO (PN)","lat":"46.0489","lng":"12.6579","address":"Via Maniago, 28","phone":"Tel. 0434 918900","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/sedrano-di-san-quirino.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"false","cafe":"false","verde":"true","diesel":"true","id":"1"},{"city":"CORTINA D'AMPEZZO (BL)","lat":"46.5413","lng":"12.1323","address":"Via dello Stadio, 7","phone":"Tel. 0436 869133","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/cortina-dampezzo.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"true","cafe":"false","verde":"true","diesel":"true","id":"2"},{"city":"POINCICCO DI ZOPPOLA (PN)","lat":"45.9487","lng":"12.7453","address":"Via Cusano, 14","phone":"Tel. 0434 574831","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/poinciccio-di-zoppola.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"true","cafe":"false","verde":"true","diesel":"true","id":"3"}]

第二步我用 jquery getJSON 数据调用,就像这样......

$.getJSON( "markers.php", function( data ) {
var station = [];
$.each( data, function( key, val ) {
station.push( data );
});
});

但这完全没用,所以...我可以用什么方式做到这一点?那么...有没有办法完成这个结构呢?

  var station = [
['SEDRANO DI SAN QUIRINO (PN)',46.048887,12.657924,'Via Maniago, 28','Tel. 0434 918900','sedrano-di-san-quirino.jpg',true,true,false,false,false,false,false],
['CORTINA D\'AMPEZZO (BL)',46.541307,12.132348,'Via dello Stadio, 7','Tel. 0436 869133','cortina-dampezzo.jpg',true,true,false,false,false,true,false],
['POINCICCO DI ZOPPOLA (PN)',45.948725,12.745332,'Via Cusano, 14','Tel. 0434 574831','poinciccio-di-zoppola.jpg',true,true,false,false,false,true,false]
];

最佳答案

var stations = []; //You need an external variable to hold all the stations

$.getJSON( "markers.php", function( data ) {
//var station = []; //Don't need this here

//"data" is an array, not a key/value object
/*$.each( data, function( key, val ) {
//You are adding the JSON "data" array here, not the values
station.push( data );
});*/

//Remember "data" is a JSON array, not object
for (var i = 0; i < data.length; i++) {
var station = []; //Temporary variable to hold station data

//Iterate over the current station object
$.each(data[i], function(key, value) {
station.push(value); //You're not interested in the key
});
stations.push(station); //Add station to array of all stations
}
});

关于javascript - 使用 JSON 创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24656193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com