将 json 字符串解析为数组[重复]

问题描述 投票:0回答:3

我想分割一个由产品组成的字符串。每个产品都包含在 {..} 内,并以逗号分隔。

例如,我下面有一个字符串。

[
{\"productid\" : \"prod:kj42j3d24-47c2-234lkj2-e3c2-cfc9a4a3b005\",\"memo\" : \"
product description
\",\"taxable\" : 0,\"unitweight\" : 0,\"unitcost\" : 0,\"unitprice\" : 12.34,\"quantity\" : 1.00},
{\"productid\" : \"prod:k324jl-462d-e589-aecf-32k4j\",\"memo\" : \"
prodict description
\",\"taxable\" : 0,\"unitweight\" : 0,\"unitcost\" : 0,\"unitprice\" : 12.23,\"quantity\" : 1}
]

并且想要将每个产品分成数组的不同索引。谢谢。

php arrays string split
3个回答
2
投票

对我来说看起来像 JSON:

$a = json_decode(" [ 
  {\"productid\" : \"prod:kj42j3d24-47c2-234lkj2-e3c2-cfc9a4a3b005\",
   \"memo\" : \" product description \",\"taxable\" : 0,\"unitweight\" : 0,
   \"unitcost\" : 0,\"unitprice\" : 12.34,\"quantity\" : 1.00}, 
  {\"productid\" : \"prod:k324jl-462d-e589-aecf-32k4j\",
   \"memo\" : \" prodict description \",\"taxable\" : 0,\"unitweight\" : 0,
   \"unitcost\" : 0,\"unitprice\" : 12.23,\"quantity\" : 1} ] ", true
);

1
投票

您的字符串看起来很像 JSON 编码的对象数组。 尝试使用 php 函数json_decode

$parsed_array = json_decode($str);

1
投票

在这种特殊情况下,您的字符串似乎已经是 JSON 格式。 PHP 对此有内置支持。要获取此字符串并将其设为 JSON 对象,请使用 json_decode 函数。

更多信息请参阅此处的 PHP 手册:http://www.php.net/manual/en/function.json-decode.php

© www.soinside.com 2019 - 2024. All rights reserved.