Javascript 如何将字符串转换为数组对象

Javascript 如何将字符串转换为数组对象,javascript,jquery,canvasjs,Javascript,Jquery,Canvasjs,我无法将字符串转换为数组对象 我的字符串看起来像[{label:“BSP”,y:3},{label:“BJP”,y:10}]“}]我希望它像 [ { label: "BSP", y: 3 }, { label: "BJP", y: 10 } ] 问题描述 我在做一个图表。我想让这个图表符合ajax的要求 我的示例代码是 jQuery.ajax({ url: baseurl+"api/", data: postData, type: 'POST', suc

我无法将字符串转换为数组对象

我的字符串看起来像
[{label:“BSP”,y:3},{label:“BJP”,y:10}]“}]
我希望它像

[
  { label: "BSP", y: 3 },
  { label: "BJP", y: 10 }
]
问题描述

我在做一个图表。我想让这个图表符合ajax的要求

我的示例代码是

jQuery.ajax({
    url: baseurl+"api/",
    data: postData,
    type: 'POST',
    success: function(html) {
        var poll_result = JSON.parse(html); // html = [{"question":"Lorem ipsum","title":"Poll Result","data_option":"[{ label: \"BSP\", y: 3 },{ label: \"BJP\", y: 10 },{ label: \"Congress\", y: 4 },{ label: \"AAP\", y: 1 },{ label: \"SP\", y: 2 },{ label: \" SP\", y: 1 }]"}]

        var data_option = poll_result.data_option.replace(/\\/g, "");
        var chart = new CanvasJS.Chart("chartContainer", {
            animationEnabled: true,
            axisX: {
                interval: 10
            },
            axisY: {
                title: poll_result.title,
            },
            data: [{
                type: "bar",

                dataPoints: data_option
                // dataPoints: [ { label: "BSP", y: 3 }, { label: "BJP", y: 10 }, { label: "Congress", y: 4 }] // Working code
            }]
        });
        chart.render();
    }
});
直接赋值变量不起作用,因为它认为它是一个字符串。所以我希望
data\u选项
与引用中给出的一样

编辑1

我的PHP数组看起来像

$output = Array
(
    [question] => Lorem ipsum
    [title] => Poll Result
    [data_option] => [{ label: "BSP", y: 3 },{ label: "BJP", y: 10 },{ label: "Congress", y: 4 },{ label: "AAP", y: 1 },{ label: "SP", y: 2 },{ label: " SP", y: 1 }]
) 
我将其转换为JSON并进行了响应

echo json_encode($output);
编辑2

我真正的代码结构是

$poll_output_result['question'] = 'Lorem ipsum';
$poll_output_result['title'] = 'Poll Result';
$poll_output_result['data_option'] = '';

foreach($poll_result as $result){
    $poll_output_result['data_option'] .= '{ label: "'.$result['user_choice'].'", y: '.$result['count'].' },';
}

$poll_output_result['data_option'] = trim($poll_output_result['data_option'], ',');
$poll_output_result['data_option'] = '['.$poll_output_result['data_option'].']';

echo json_encode($poll_output_result);

您最初的问题是从ajax请求返回JSON答案,相反,您会收到plein文本。这就是为什么您必须JSON.parse您的内容

要指定来自服务器的应答类型,必须在应答中添加正确的标题

在PHP中要做的事情:

//In top of your php file
header('Content-Type: application/json'); 
//[...]
echo json_encode($myArray);

编辑1

因为您使用的是JSON_ENCODE,所以您不需要编写自己的JSON,让PHP为您自己编写。用以下代码替换您的代码:

$poll_output_result['question'] = 'Lorem ipsum';
$poll_output_result['title'] = 'Poll Result';
$poll_output_result['data_option'] = [];

foreach($poll_result as $result){
    $poll_output_result['data_option'][] = [ 
        'label' => $result['user_choice'],
        'y' =>  $result['count']
    ];
}

echo json_encode($poll_output_result);

为什么不使用兼容的字符串呢?我在PHP代码中将数组转换为JSON字符串。但是它看起来不像标准的JSON,例如带有引号的键。PHP有JSON_encode()等@Noface我曾使用相同的方法将数组转换为JSON格式,但JSON的答案在
JSON.parse(html)
我有疑问,你的键中解码得很好。”PHP数组上的data_option“将json字符串化为值?或真实的PHP结构?好的。让我在问题中添加真实的PHP代码结构