尝试将阵列包装到json_encode()
:
function add_map_data() {
$objName = "MapData";
$array = array(
"MapViewLatitude" => "51.505",
"MapViewLongitude" => "-0.09",
);
wp_enqueue_script( \'mapdata\', get_bloginfo(\'template_url\').\'/custom/map.js\', null, null, false );
wp_localize_script( \'mapdata\', $objName, array(\'my_arr\' =>json_encode($array)));
}
add_action( \'wp_enqueue_scripts\', \'add_map_data\');
下一步是将JSON字符串解析回JavaScript中的JSON对象,以便:
//first escape the string then parse this string and convert it into a JSON object
var MapDataJSON = jQuery.parseJSON(MapData.my_arr.replace(/"/g, \'"\'));