求帮忙。很奇怪的问题,我想不通了。JSON数组。

前云 2015-01-04 04:06:06
[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]


这是JSON代码,没有问题吧。

	
$product_info=str_replace('"','"',$ship_info["product_info"]);//替换转义字符 (双引号)
$product_info=json_decode($product_info,true);
print_r($product_info);




上面的代码貌似也没什么问题。

问题是
当JSON里面办有一组数据的时候就很正常。$product_info 里面就有数据。和预期的一样。
[{"id":"9","model":"A2","quantity":"1"}]


当JSON里面有2组数据的时候像下面这样,$product_info 就为空。什么都没有。
[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]


我代码哪里写错了。

还有当前没有执行 json_decode之前。字符串是正常的。
...全文
177 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2015-01-04
  • 打赏
  • 举报
回复
请先看清楚你的真实数据再考虑写代码!
$s = 'W3smcXVvdDtpZCZxdW90OzomcXVvdDs5JnF1b3Q7LCZxdW90O21vZGVsJnF1b3Q7OiZxdW90O0EyJnF1b3Q7LCZxdW90O3F1YW50aXR5JnF1b3Q7OiZxdW90OzEmcXVvdDt9LHsmcXVvdDtpZCZxdW90OzomcXVvdDs4JnF1b3Q7LCZxdW90O21vZGVsJnF1b3Q7OiZxdW90O0ExJnF1b3Q7LCZxdW90O3F1YW50aXR5JnF1b3Q7OiZxdW90OzEmcXVvdDt9XQ==';
$s = base64_decode($s);
$s = html_entity_decode($s);
$s = html_entity_decode($s); //多做几遍也无所谓,关键是要一层一层的将 html 转义转回来
echo $s;
print_r(json_decode($s, 1));
[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]
Array
(
    [0] => Array
        (
            [id] => 9
            [model] => A2
            [quantity] => 1
        )

    [1] => Array
        (
            [id] => 8
            [model] => A1
            [quantity] => 1
        )

)

前云 2015-01-04
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
$s = '[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]';
print_r(json_decode($s, 1));
Array
(
    [0] => Array
        (
            [id] => 9
            [model] => A2
            [quantity] => 1
        )

    [1] => Array
        (
            [id] => 8
            [model] => A1
            [quantity] => 1
        )

)

没有问题 你应该贴出未经处理的原始数据,才可能发现问题 比如数据库取出的是 $ship_info["product_info"] 那就 echo base64_encode($ship_info["product_info"]); 贴出结果
$product_info=str_replace('"','"',$ship_info["product_info"]);
这行代码的问题。" 这样写一组数据的时候可以用。多组为空 " 这样写多组的正常组的。单组的为空
前云 2015-01-04
  • 打赏
  • 举报
回复
原始数据: [{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]

$product_info=str_replace('"','"',$ship_info["product_info"]);//替换转义字符 (双引号)
上面的数据是经这个替换的。
但为什么JSON里只有一级数据的时候是正常的呢。

前云 2015-01-04
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
$s = '[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]';
print_r(json_decode($s, 1));
Array
(
    [0] => Array
        (
            [id] => 9
            [model] => A2
            [quantity] => 1
        )

    [1] => Array
        (
            [id] => 8
            [model] => A1
            [quantity] => 1
        )

)

没有问题 你应该贴出未经处理的原始数据,才可能发现问题 比如数据库取出的是 $ship_info["product_info"] 那就 echo base64_encode($ship_info["product_info"]); 贴出结果
下面是显示的结果 echo base64_encode($ship_info["product_info"]); W3smcXVvdDtpZCZxdW90OzomcXVvdDs5JnF1b3Q7LCZxdW90O21vZGVsJnF1b3Q7OiZxdW90O0EyJnF1b3Q7LCZxdW90O3F1YW50aXR5JnF1b3Q7OiZxdW90OzEmcXVvdDt9LHsmcXVvdDtpZCZxdW90OzomcXVvdDs4JnF1b3Q7LCZxdW90O21vZGVsJnF1b3Q7OiZxdW90O0ExJnF1b3Q7LCZxdW90O3F1YW50aXR5JnF1b3Q7OiZxdW90OzEmcXVvdDt9XQ== echo $ship_info["product_info"]; [{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]
xuzuning 2015-01-04
  • 打赏
  • 举报
回复
$s = '[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]';
print_r(json_decode($s, 1));
Array
(
    [0] => Array
        (
            [id] => 9
            [model] => A2
            [quantity] => 1
        )

    [1] => Array
        (
            [id] => 8
            [model] => A1
            [quantity] => 1
        )

)

没有问题 你应该贴出未经处理的原始数据,才可能发现问题 比如数据库取出的是 $ship_info["product_info"] 那就 echo base64_encode($ship_info["product_info"]); 贴出结果

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧