【发布时间】:2020-12-19 19:12:23
【问题描述】:
我以将我的 javascript localstorage 转换为发布请求的形式发布了帖子。从那里我尝试解码我的 json 字符串以在 PHP 中创建一个对象。
我的 php 代码在回显之前的样子
$cart_items = $_POST['cart_items'];
$cart_items = json_encode($cart_items);
$array_test = json_decode($cart_items);
print_r($array_test);
它在浏览器中返回的内容
[{\"id\":83494890,\"title\":\"2020 Hino 358\",\"partType\":\"Bumpers\",\"price\":100,\"stockNumber\":12313131312,\"thumbImg\":\"/jOIY91KhEby8_f.jpg\",\"permalink\":\"/part-description/?part=83494890\",\"maxQuantity\":1,\"requestedQuantity\":\"3\"}
,{\"id\":83493833,\"title\":\"2009 Freightliner 5020080\",\"partType\":\"ABS Modulator Valves\",\"price\":150,\"stockNumber\":\"P-1211111111157\",\"thumbImg\":\"/OOjQbsi6p8kX_f.jpg\",\"permalink\":\"/part-description/?part=83493833\",\"maxQuantity\":1,\"requestedQuantity\":\"1\"}]
我知道,通常在查看 json 数据时,处处都没有正斜杠。我试图将json_decode成一个数组而不是一个对象,然后为里面的每个对象创建一个foreach。但我收到此错误返回“为 foreach() 提供的参数无效”
如何使这个 json 字符串转换为对象数组?谢谢
【问题讨论】:
-
您能在此之前
var_dump($_POST);并在这里分享 POST 的内容吗?