PHP:编码嵌套的JSON数组问题(PHP : Encoding nested JSON array issue)

编程入门 行业动态 更新时间:2024-10-12 03:19:03
PHP:编码嵌套的JSON数组问题(PHP : Encoding nested JSON array issue)

我很困惑写这一个,我怎样才能实现这个类似的输出。 我从早就开始做json编码。 但这个对我来说是不同的。

[{ type: 'pie', name: 'Browser share', innerSize: '40%', data: [ ['Firefox', 10.38], ['IE', 56.33], ['Chrome', 24.03], ['Safari', 4.77], ['Opera', 0.91], {//over here i can't get this braces name: 'Proprietary or Undetectable', y: 0.2, dataLabels: { enabled: false } } ] }]

这是我的代码,

$percentage = array(); $arr2 = new stdClass(); $arr2->type = 'pie'; $arr2->name = 'Browser share'; $arr2->innerSize = '40%'; while($azSoc = mysql_fetch_assoc($az)){ $arr2->data[] = array("".$azSoc['criteria_name']."", $azSoc['crit_score_percentage']); } $arr2->name = 'Porperty ok'; $arr2->y = 0.2; $arr2->dataLabels[] = array('enable',false); array_push($percentage, $arr2); echo json_encode( $percentage);

我的代码输出给了我,

[{"type":"pie","name":"Porperty ok","innerSize":"40%","data":[["tes","25"], ["awdawdas","25"],["awdawd","25"],["ehhehe","25"]],"y":0.2,"dataLabels": [["enable",false]]}]

I am confused writing this one, how can i achieve achieve this similar output. I'm doing json encode since early. But this one is different for me.

[{ type: 'pie', name: 'Browser share', innerSize: '40%', data: [ ['Firefox', 10.38], ['IE', 56.33], ['Chrome', 24.03], ['Safari', 4.77], ['Opera', 0.91], {//over here i can't get this braces name: 'Proprietary or Undetectable', y: 0.2, dataLabels: { enabled: false } } ] }]

Here is my code,

$percentage = array(); $arr2 = new stdClass(); $arr2->type = 'pie'; $arr2->name = 'Browser share'; $arr2->innerSize = '40%'; while($azSoc = mysql_fetch_assoc($az)){ $arr2->data[] = array("".$azSoc['criteria_name']."", $azSoc['crit_score_percentage']); } $arr2->name = 'Porperty ok'; $arr2->y = 0.2; $arr2->dataLabels[] = array('enable',false); array_push($percentage, $arr2); echo json_encode( $percentage);

The output of my codes gives me,

[{"type":"pie","name":"Porperty ok","innerSize":"40%","data":[["tes","25"], ["awdawdas","25"],["awdawd","25"],["ehhehe","25"]],"y":0.2,"dataLabels": [["enable",false]]}]

最满意答案

你想要实现的是无效的json。 您需要在名为some_name数组中创建一个新的some_name ,然后将该对象加载到该出现中

$percentage = array(); $arr2 = new stdClass(); $arr2->type = 'pie'; $arr2->name = 'Browser share'; $arr2->innerSize = '40%'; while($azSoc = mysql_fetch_assoc($az)){ $arr2->data[] = array("".$azSoc['criteria_name']."", $azSoc['crit_score_percentage']); } $t = new stdClass(); $t->name = 'Porperty ok'; $t->y = 0.2; $t->dataLabels = array('enable',false); $arr2->data['some_name'] = $t; $percentage[] = $arr2; //dont know where $cat came from I assume its a larger oject you are building echo json_encode( $cat);

What you are trying to achieve is not valid json. You need to create a new occurance in your array with a name some_name and then load the object into that occurance

$percentage = array(); $arr2 = new stdClass(); $arr2->type = 'pie'; $arr2->name = 'Browser share'; $arr2->innerSize = '40%'; while($azSoc = mysql_fetch_assoc($az)){ $arr2->data[] = array("".$azSoc['criteria_name']."", $azSoc['crit_score_percentage']); } $t = new stdClass(); $t->name = 'Porperty ok'; $t->y = 0.2; $t->dataLabels = array('enable',false); $arr2->data['some_name'] = $t; $percentage[] = $arr2; //dont know where $cat came from I assume its a larger oject you are building echo json_encode( $cat);

更多推荐

本文发布于:2023-08-07 16:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   JSON   PHP   issue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!