简体   繁体   English

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

[英]How to convert string into array object

I have problem with converting string into array object. 我在将字符串转换为数组对象时遇到问题。

My string looks like [{ label: "BSP", y: 3 },{ label: "BJP", y: 10 }]"}] I want it to be like 我的字符串看起来像[{ label: "BSP", y: 3 },{ label: "BJP", y: 10 }]"}]我希望它像

[
  { label: "BSP", y: 3 },
  { label: "BJP", y: 10 }
]

Problem description 问题描述

I was working on a chart Canvas Js . 我正在制作Canvas Js图表。 I want the chart on some ajax request. 我想要一些ajax请求上的图表。

My sample code is 我的示例代码是

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();
    }
});

Assigning variable directly is not working as I thing it considers it as a string. 直接分配变量是行不通的,因为我认为它是字符串。 So I want the data_option to be look like as given on the reference. 因此,我希望data_option看起来像参考中给出的那样。

Edit 1 编辑1

My PHP array looks like 我的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 }]
) 

which I converted into JSON and echoed. 我转换成JSON并回显了它。

echo json_encode($output);

Edit 2 编辑2

My Real code structure is 我的真实代码结构是

$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);

You original issue is return JSON answer from ajax request, instead, you receive plein text. 您最初的问题是从ajax请求返回JSON答案,相反,您收到了平淡的文本。 Is why you have to JSON.parse your content. 这就是为什么您必须JSON.parse您的内容的原因。

To specify type of answer from your server, you have to add the right header to your response. 要从服务器指定答案的类型,您必须在响应中添加正确的标题。

TODO that in PHP : 在PHP中执行以下操作:

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

EDIT 1 编辑1

Because you use JSON_ENCODE, you don't need to craft your own json, let PHP do it for your. 因为您使用JSON_ENCODE,所以不需要制作自己的json,因此让PHP为您完成。 Replace your code by this : 用以下代码替换您的代码:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM