Javascript JSON解析赢得';t转换为数组

Javascript JSON解析赢得';t转换为数组,javascript,google-maps-api-3,Javascript,Google Maps Api 3,在我的代码中,我想解析JSON数据并显示Google Maps信息窗口,但当我通过JSON.parse()函数进行转换并将其作为变量传递时,信息窗口将无法正常工作,但当我使用JSON.parse函数转换并在textarea中显示结果,然后手动放置转换后的值时,它会正确地显示信息窗口 下面是我通过AJAX/JSON Raw返回的内容 [ "['<div class=\"info_content\"><h3>test 1<\/h3><h4>Co

在我的代码中,我想解析JSON数据并显示Google Maps信息窗口,但当我通过
JSON.parse()
函数进行转换并将其作为变量传递时,信息窗口将无法正常工作,但当我使用
JSON.parse
函数转换并在
textarea
中显示结果,然后手动放置转换后的值时,它会正确地显示信息窗口

下面是我通过AJAX/JSON Raw返回的内容

[
    "['<div class=\"info_content\"><h3>test 1<\/h3><h4>Contact: 33<\/h4><p>33<\/p><\/div>']",
    "['<div class=\"info_content\"><h3>test 2<\/h3><h4>Contact: 22<\/h4><p>22<\/p><\/div>']",
    "['<div class=\"info_content\"><h3>test 3<\/h3><h4>Contact: 55<\/h4><p>55<\/p><\/div>']"
]
完整的JS代码

<script>
    var markers = [];

    function initMap(location_data, location_box) {
        var map;
        var bounds = new google.maps.LatLngBounds();
        var mapOptions = {
            mapTypeId: 'roadmap',
            center: new google.maps.LatLng(54.57951, -4.41387),
        };

        // Display a map on the web page
        map = new google.maps.Map(document.getElementById("map"), mapOptions);
        map.setTilt(50);

        // Multiple markers location, latitude, and longitude
        //alert(location_data);
        var markers = location_data;
        //var markers = [['test 1', 25.1212, 55.1535, 5],['test 2', 25.2084, 55.2719, 6],['test 3', 25.2285, 55.3273, 7]];

        var infoWindowContent = location_box;

        // Add multiple markers to map
        var infoWindow = new google.maps.InfoWindow(),
            marker, i;

        // Place each marker on the map  
        for (i = 0; i < markers.length; i++) {
            var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
            bounds.extend(position);
            marker = new google.maps.Marker({
                position: position,
                map: map,
                title: markers[i][0]
            });

            // Add info window to marker    
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infoWindow.setContent(infoWindowContent[i][0]);
                    infoWindow.open(map, marker);
                }
            })(marker, i));

            // Center the map to fit all markers on the screen
            map.fitBounds(bounds);
        }

        // Set zoom level
        var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
            this.setZoom(15);
            google.maps.event.removeListener(boundsListener);
        });

    }

    $(document).ready(function() {
        var location_data;
        $("#mapped").on("change", function() {
            var dataname = $(".selectpicker option:selected").val();
            $.ajax({
                url: "findforwork.php",
                type: "POST",
                data: "searchid=" + dataname,
                success: function(response) {
                    //alert('Success' + response);
                    var str_response = response;
                    var res_new = str_response.split("==============");
                    var location_data = JSON.parse(res_new[0].replace(/\"/g, "").replace(/\'/g, "\""));
                    var location_box = res_new[1]; // Info windows Array will be here
                    $('#infoBx').val(location_box);
                    var mapDiv = document.getElementById('map');
                    google.maps.event.addDomListener(mapDiv, "load", initMap(location_data, location_box));
                }
            }); //End Of Ajax
        }); //End of mapped
    });
</script>

var标记=[];
函数初始化映射(位置数据,位置框){
var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
mapTypeId:“路线图”,
中心:新google.maps.LatLng(54.57951,-4.41387),
};
//在网页上显示地图
map=new google.maps.map(document.getElementById(“map”)、mapOptions);
地图设置倾斜(50);
//多个标记位置、纬度和经度
//警报(位置数据);
var markers=位置和数据;
//var标记=['测试1',25.1212,55.1535,5],'测试2',25.2084,55.2719,6],'测试3',25.2285,55.3273,7];
var infoWindowContent=位置\框;
//向地图添加多个标记
var infoWindow=new google.maps.infoWindow(),
马克,我;
//将每个标记放在地图上
对于(i=0;i
生成JSON数据的PHP代码

$locations  = array();
$locas      = array();
$infoDialog = array();
if (is_numeric($_POST['searchid'])) {
    $service_id = $_POST['searchid'];
    $query      = "SELECT * FROM tblemployees WHERE FIND_IN_SET($service_id, service)";
    if (!$result = mysqli_query($conn, $query)) {
        exit(mysqli_error($conn));
    }

    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_array($result)) {
            // username is already exist 

            $print_info = null;

            $latitude       = $row['ltd'];
            $longitude      = $row['lotd'];
            $person_name    = $row['fullname'];
            $person_id      = $row['id'];
            $person_contact = $row['contact'];
            $person_address = $row['address'];

            $locations[] = array(
                'name' => $person_name,
                'lat' => $latitude,
                'lng' => $longitude,
                'lnk' => $person_id
            );
            $locas[]     = "['" . $person_name . "', " . $latitude . ", " . $longitude . ", " . $person_id . "]";

            $print_info .= '<div class="info_content">';
            $print_info .= '<h3>' . $person_name . '</h3>';
            $print_info .= '<h4>Contact: ' . $person_contact . '</h4>';
            $print_info .= '<p>' . $person_address . '</p>';
            $print_info .= '</div>';

            $infoDialog[] = "['" . $print_info . "']";
        }
    }

    $json_response = json_encode($locas);
    $json_info     = json_encode($infoDialog);
    echo $json_response . "==============" . $json_info;
}
$locations=array();
$locas=array();
$infoDialog=array();
如果(是数字($\u POST['searchid'])){
$service\u id=$\u POST['searchid'];
$query=“从tblemployees中选择*,其中在集合中查找($service\u id,service)”;
if(!$result=mysqli\u查询($conn,$query)){
退出(mysqli_错误($conn));
}
如果(mysqli_num_行($result)>0){
while($row=mysqli\u fetch\u数组($result)){
//用户名已存在
$print_info=null;
$LATIONE=$row['ltd'];
$longitude=$row['lotd'];
$person_name=$row['fullname'];
$person_id=$row['id'];
$person_contact=$row['contact'];
$person_address=$row['address'];
$locations[]=数组(
'name'=>$person\u name,
“纬度”=>$LATIONE,
“lng”=>$longitude,
“lnk”=>$person\u id
);
$locas[]=“['”、“$person\u name.”、“$latitude.”、“$latitude.”、“$latitude.”、“$person\u id.””;
$print_info.='';
$print_info.=''.$person_name.';
$print_info.=“联系人:”.$person_Contact.”;
$print_info.=''.$person_address.

'; $print_info.=''; $infoDialog[]=“['”$print_info.“']”; } } $json_response=json_encode($locas); $json_info=json_encode($infoDialog); echo$json_响应。“===================”$json_信息; }
不清楚响应是数组还是字符串。如果是字符串数组,则应解析每个成员,例如:

var location_box = response.map(x=>JSON.parse(x)); 
这样,位置框将是一个由一个元素数组组成的数组

编辑

根据您的代码,响应变量是一个字符串,因此,首先解析它:

var location_box = JSON.parse(res_new[0]).map(x=>JSON.parse(x));

你能在这里发布原始JSON对象吗?你是如何得到响应的?似乎生成响应的内容不正确。@jburtondev请检查我的更新question@KevinB我正在学习php,你的php不正确。在构建整个阵列之后,您应该只使用json_编码一次。目前您正在对每个项目进行json_编码,然后对整个项目进行json_编码。请检查我的完整JS代码,这可能会对您有所帮助—对我来说,似乎必须首先解析响应本身:
json.parse(response.map)(json.parse)
@Rtra您的
响应
字符串到底是什么样子的?我得到了这个未捕获的语法错误:U
var location_box = response.map(x=>JSON.parse(x)); 
var location_box = JSON.parse(res_new[0]).map(x=>JSON.parse(x));