• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

PHP数组使用 php_encode 后再使用 JSON.parse 转换成js对象会报错(之前数组中有个值是 json字符串)

PHP admin 12个月前 (06-08) 7次浏览 已收录 0个评论
文章目录[隐藏]

数组:


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
        )

)

json :

[{"productid":"2","cateid":"4","title":"u8863","descr":"u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416","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"}]

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

Unexpected number in JSON at position 169

回复内容:

数组:


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
        )

)

json :

[{"productid":"2","cateid":"4","title":"u8863","descr":"u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416","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"}]

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

Unexpected number in JSON at position 169

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

$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);
[{"productid":2,"cateid":4,"title":"u8863","descr":"u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416u5416","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}]

$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='';
console.log(JSON.parse(json));


39MO , 版权所有丨如未注明 , 均为原创丨转载请注明原文链接:PHP数组使用 php_encode 后再使用 JSON.parse 转换成js对象会报错(之前数组中有个值是 json字符串)
喜欢 (0)
[1353713598@qq.com]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址