【发布时间】:2016-12-07 14:12:45
【问题描述】:
我正在关注以下指南: https://developers.google.com/chart/interactive/docs/php_example
他们为 json 示例提供了一个 json sn-p。你如何用php构建这个?通常只是使用 json_encode() 将数组转换为 json,但这次似乎您也需要一个对象。任何人都可以澄清这一点吗?
json sn-p:
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}
到目前为止我所拥有的:
$obj = new stdClass();
$obj->cols= array(
array("id"=>"", "label"=>"Topping", "pattern"=>"", "type"=>"string"),
array("id"=>"", "label"=>"Slices", "pattern"=>"", "type"=>"string"));
$obj->rows = array(
array()
);
echo json_encode($obj);
有没有人知道如何完成这个 php 表示?
编辑: 我的回声输出:
{"cols":[{"id":"","label":"Topping","pattern":"","type":"string"},{"id":"","label":"Slices","pattern":"","type":"string"}],"rows":[[]]}
【问题讨论】:
-
那么你的代码的结果是什么?
-
PHP 关联数组会转换成 JSON 对象,你不需要
stdClass -
@JulianKuchlbauer,对不起。我已经更新了问题并插入了我的输出
-
@ndm,那么你会在数组上使用什么结构?
-
@NDM 如果你想让你的代码自我记录,你没有理由不使用
stdClass
标签: php json google-visualization