简体   繁体   English

json_encode(json_encode(array))

[英]json_encode( json_encode (array) )

I'm trying to create a PHP script to generate JSON data for a jqplot bubble chart. 我正在尝试创建一个PHP脚本来为jqplot气泡图生成JSON数据。 The jqplot sample code requires data in the format jqplot 示例代码需要以下格式的数据

var arr = [ 
  [45, 92, 1067, {label:"Alfa Romeo", color:'skyblue'}], 
  etc.
];

My script is along the lines of 我的剧本是

while ...
  array_push(
    $arrBubble, 
    array(
      11, 
      123, 
      1236,
      json_encode(
        array('label' => $car, 'color' => 'skyblue')
      )
  );
} 
echo json_encode($arrBubble);

The problem is that the result is 问题是结果是

[ [11, 123, 1236, "{\"label\":"VW", \"color\":\"skyblue\"}"] ]

The double json_encode has encoded the object(?) as a literal string. 双重json_encode已将object(?)编码为文字字符串。

What's the best way to work around this? 解决此问题的最佳方法是什么?

There is no reason to explicitly have a json_encode for one of the values inside the array. 没有理由为数组中的值之一明确拥有json_encode When you're using json_encode , it'll convert each level of the array as you expect. 当您使用json_encode ,它将按您期望的那样转换数组的每个级别。

var_dump(json_encode([
  11, 
  123, 
  1236,
  ['label' => $car, 'color' => 'skyblue']
]));

Outputs the structure you want: 输出所需的结构:

string(48) "[11,123,1236,{"label":"VW","color":"skyblue"}]"

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

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