0

我需要你们帮助伙计们。我试图将 json 响应解析为 highcharts 可以呈现为柱形图的内容。我已经搜索和搜索没有任何运气。

这是呈现图表之前 json 数据应如何显示的示例:

[
[1,12],
[2,5],
[3,18],
[4,13],
[5,7],
[6,4],
[7,9],
[8,10],
[9,15],
[10,22]
]

这就是我的 json 响应的样子:

{"col":["name","tot"],"grp":{"wkd":[["","0"],["id-1","0"],["id-2","0"],["id-3","0"],["id-4","0"],["id-5","0"],["id-6","0"],["id-7","0"]]},"Spec":{}}

我需要从响应中获取的唯一信息是 id-1 到 7 及其值。

我使用 curl 针对带有令牌和秘密的 url 发送发布请求。

这是代码的 curl 部分和 json_decode:

$ch = curl_init($signedUrl);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json',
                                           'Accept-language: en-GB',
                                           'Content-type: application/json',                                                                                
                                           'Content-length: ' . strlen($data_string))); 

$json = curl_exec($ch);

print "RESPONSE BODY (as string (JSON)):<br/><br/>" . $json;
echo "<br><br>";

$data = json_decode($json, true);

curl_close($ch);

我不知道如何解析 json_decode 之后的信息。我已经尝试了多个 foreach 字符串,但没有任何运气。谁能帮我解决这个问题?

问候菲利普

4

1 回答 1

1

您想分解对象的相关部分,循环遍历,如果填充了 id,则将其作为数据抓取。然后你需要重新 json_encode 它在图表中使用:

$json       = '{"col":["name","tot"],"grp":{"wkd":[["","0"],["id-1","0"],["id-2","0"],["id-3","0"],["id-4","0"],["id-5","0"],["id-6","0"],["id-7","0"]]},"Spec":{}}';
$decoded    = json_decode($json);
$values     = $decoded->grp->wkd;

$i = 0;
foreach($values as $k=>$arr) {
    $id = $arr[0];
    if(!empty($id)) {
        $data[$i][]     = $i;
        $data[$i][]     = $arr[1];
        $cats[$i]       = $id;
        $i++;
    }
}
$data = json_encode($data);
$cats = json_encode($cats);

如果需要,这还包括一个用于构建类别数组的部分。如果没有,就直接省略。

该例程的输出是:

$数据:

[[0,"0"],[1,"0"],[2,"0"],[3,"0"],[4,"0"],[5,"0"],[6,"0"]] 

$猫:

["id-1","id-2","id-3","id-4","id-5","id-6","id-7"]
于 2013-09-17T15:13:10.877 回答