Using php_encode for a PHP array and then using JSON.parse to convert it into a js object will result in an error (previously one of the values ​​in the array was a json string)

WBOY
Release: 2023-03-02 06:42:01
Original
1310 people have browsed it

数组:

<code>
Array
(
    [0] => Array
        (
            [productid] => 2
            [cateid] => 4
            [title] => 衣
            [descr] => 吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖
            [num] => 197
            [price] => 888.00
            [cover] => 7xpizy.com1.z0.glb.clouddn.com/58087fa893aa7
            [pics] => {"58087faa67f8b":"7xpizy.com1.z0.glb.clouddn.com\/58087faa67f8b"}
            [issale] => 1
            [saleprice] => 799.00
            [ishot] => 1
            [istui] => 1
            [ison] => 1
            [createtime] => 0
        )

)</code>
Copy after login
Copy after login

json :

<code class="json">[{"productid":"2","cateid":"4","title":"\u8863","descr":"\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416","num":"197","price":"888.00","cover":"7xpizy.com1.z0.glb.clouddn.com\/58087fa893aa7","pics":"{\"58087faa67f8b\":\"7xpizy.com1.z0.glb.clouddn.com\\\/58087faa67f8b\"}","issale":"1","saleprice":"799.00","ishot":"1","istui":"0","ison":"1","createtime":"0"}]</code>
Copy after login
Copy after login

数组中的 pics 本来就是json了,然后 json_encode 后在js中 JSON.parse 会报错:

<code>Unexpected number in JSON at position 169</code>
Copy after login
Copy after login

回复内容:

数组:

<code>
Array
(
    [0] => Array
        (
            [productid] => 2
            [cateid] => 4
            [title] => 衣
            [descr] => 吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖
            [num] => 197
            [price] => 888.00
            [cover] => 7xpizy.com1.z0.glb.clouddn.com/58087fa893aa7
            [pics] => {"58087faa67f8b":"7xpizy.com1.z0.glb.clouddn.com\/58087faa67f8b"}
            [issale] => 1
            [saleprice] => 799.00
            [ishot] => 1
            [istui] => 1
            [ison] => 1
            [createtime] => 0
        )

)</code>
Copy after login
Copy after login

json :

<code class="json">[{"productid":"2","cateid":"4","title":"\u8863","descr":"\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416","num":"197","price":"888.00","cover":"7xpizy.com1.z0.glb.clouddn.com\/58087fa893aa7","pics":"{\"58087faa67f8b\":\"7xpizy.com1.z0.glb.clouddn.com\\\/58087faa67f8b\"}","issale":"1","saleprice":"799.00","ishot":"1","istui":"0","ison":"1","createtime":"0"}]</code>
Copy after login
Copy after login

数组中的 pics 本来就是json了,然后 json_encode 后在js中 JSON.parse 会报错:

<code>Unexpected number in JSON at position 169</code>
Copy after login
Copy after login

对原数组进行处理,json部分转成数组

<code>$arr = Array(Array
        ("productid" => 2,
            "cateid" => 4,
            "title" => "衣",
            "descr" => "吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖",
            "num" => 197,
            "price" => 888.00,
            "cover" => "7xpizy.com1.z0.glb.clouddn.com/58087fa893aa7",
            "pics" => '{"58087faa67f8b":"7xpizy.com1.z0.glb.clouddn.com\/58087faa67f8b"}',
            "issale" => 1,
            "saleprice" => 799.00,
            "ishot" => 1,
            "istui" => 1,
            "ison" => 1,
            "createtime" => 0
        )
    );

foreach($arr as $k=>$v){
    $arr[$k]['pics'] = array(json_decode($arr[$k]['pics'],true));
}
echo json_encode($arr);</code>
Copy after login
<code>[{"productid":2,"cateid":4,"title":"\u8863","descr":"\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416","num":197,"price":888,"cover":"7xpizy.com1.z0.glb.clouddn.com\/58087fa893aa7","pics":[{"58087faa67f8b":"7xpizy.com1.z0.glb.clouddn.com\/58087faa67f8b"}],"issale":1,"saleprice":799,"ishot":1,"istui":1,"ison":1,"createtime":0}]</code>
Copy after login

<code class="php">$arr = [[
        
            'productid' => 2,
            'cateid' => 4,
            'title' => '衣',
            'descr' => '吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖吖',
            'num' => 197,
            'price' => 888.00,
            'cover' => '7xpizy.com1.z0.glb.clouddn.com/58087fa893aa7',
            'pics' => '{"58087faa67f8b":"7xpizy.com1.z0.glb.clouddn.com\/58087faa67f8b"}',
            'issale' => 1,
            'saleprice' => 799.00,
            'ishot' => 1,
            'istui' => 1,
            'ison' => 1,
            'createtime' => 0,
        

]];

$arr[0]['pics'] = json_decode($arr[0]['pics'],true);

$json=json_encode($arr);

//以下为js 正常输出
var json='<?php echo $json;?>';
console.log(JSON.parse(json));</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template