在 PHP Zend 2 中从 POST-request 解码 JSON-String


Decode JSON-String from POST-request in PHP Zend 2

我真的需要帮助。

json_decode返回 NULL。

我从开机自检中获取字符串:

$arrayOfItems = $request->getPost()->get('arrayOfItems'); 

字符串如下所示:

'[{id: 161, value1: 1, value2: 1},{id: 162, value1: 2, value2: 2},{id: 163, value1: 3, value2: 3}]'

我尝试将 id 放入数组中:

$decodedArray = json_decode($arrayOfItems);
$ids = array();
foreach ($decodedArray as $v) {
    $ids[] = $v->id;
}

但json_decode返回 NULL。

欢迎任何帮助。

亲切问候霍尔特曼

$arrayOfItems中的键必须是字符串,json_decode才能在这里工作。 例如,以下方法有效:

'[{"id": 161, "value1": 1, "value2": 1},{"id": 162, "value1": 2, "value2": 2},{"id": 163, "value1": 3, "value2": 3}]'

这里可能有几件事。 首先,我不确定这是有效的 JSON。

其次,如果$arrayOfItems的输出中有引号,它将不起作用。

如果您可以控制要从中请求的 API,请检查它的输出并找出它为什么有单引号。 如果没有,您可以正则表达式掉单引号,但根据您的输出,您应该小心这一点,因为您可能需要它们。