[英]Error converting Json string to array of objects PHP
I made a post in a form converting my javascript localstorage to a post request.我以将我的 javascript localstorage 转换为 post 请求的形式发布了一个帖子。 From there I tried to decode my json string to make an object in PHP.从那里我尝试解码我的 json 字符串以在 PHP 中创建一个对象。
How my php code looks before I echo it我的 php 代码在回显之前的样子
$cart_items = $_POST['cart_items'];
$cart_items = json_encode($cart_items);
$array_test = json_decode($cart_items);
print_r($array_test);
What it returns in browser它在浏览器中返回什么
[{\"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\"}]
I know that typically when seeing json data there isn't forward slashes everywhere.我知道通常在查看 json 数据时不会到处都有正斜杠。 I tried to json_decode into an array rather than an object, then make a foreach for each object inside.我试图将 json_decode 转换成一个数组而不是一个对象,然后为里面的每个对象创建一个 foreach。 But I got this error returned " Invalid argument supplied for foreach() "但是我得到了这个错误返回“为 foreach() 提供的无效参数”
How do I make this json string convert to an array of objects?如何使这个 json 字符串转换为对象数组? Thank you谢谢
The problem I was having was when I was getting the $_POST[] it was using PHP's "magic quotes" which was giving me improper format for my json.我遇到的问题是,当我获得 $_POST[] 时,它使用了 PHP 的“魔术引号”,这使我的 json 格式不正确。 That being said, after disabling this, it removes the slashes.话虽如此,禁用此功能后,它会删除斜杠。
It looks like $_POST['cart_items']
already contains JSON.看起来$_POST['cart_items']
已经包含 JSON。 So you just need to decode it, not encode it first.所以你只需要解码它,而不是先编码它。
$array_test = json_decode($_POST['cart_items'], true);
print_r($array_test);
But it's actually encoded twice, that's why it has escaped quotes, so you need to call json_decode()
twice.但它实际上被编码了两次,这就是它转义引号的原因,所以你需要调用json_decode()
两次。 But it's missing the double quotes around the whole thing, and the embedded newline is not valid.但是它缺少整个事情的双引号,并且嵌入的换行符无效。
The following works:以下工作:
<?php
$cart_items = '"[{\"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\"}]"';
$array_test = json_decode(json_decode($cart_items));
print_r($array_test);
I suggest you find the code that's sending the cart_item
POST parameter and fix it so it doesn't do all this extra encoding.我建议你找到发送cart_item
POST 参数的代码并修复它,这样它就不会执行所有这些额外的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.