[英]how can convert json object to array in php?
I have this JSON我有这个 JSON
[size: null, color: "white"]
Which send to the server via post method.通过post方法发送到服务器。
I try我试试
$your_json_string = json_decode($your_json_string, TRUE)
and和
$your_json_string = html_entity_decode($your_json_string);
$your_json_string = json_decode($your_json_string, true);
print_r($your_json_string);
使用print_r($your_json_string);
I get: null .我得到: null 。echo json_last_error();
随着echo json_last_error();
I get: 4 .我得到: 4 。Any ideas on how I can solve this?关于如何解决这个问题的任何想法?
1) that is a json array [] not an object {}. 1) 那是一个 json 数组 [] 而不是对象 {}。 2) it will want property names quoted. 2) 它需要引用属性名称。
$String = <<< LOL
{"size": null, "color": "white"}
LOL;
print_r(json_decode($String,TRUE));
then you get然后你得到
Array
(
[size] =>
[color] => white
)
If you have a wrong json string and you don't know how it will convert json to array in php.如果你有一个错误的 json 字符串并且你不知道它如何将 json 转换为 php 中的数组。 Example Below:示例如下:
$string = '{test ing,test ingredients,test ingredients3,test ingredients4,test ingredients5}';
Trim your json string and remove non required data.修剪您的 json 字符串并删除不需要的数据。
$string = ltrim($string, '{');
$string = rtrim($string, '}');
Remove comma from string and get normal php array从字符串中删除逗号并获得正常的php数组
$newSting = explode(',', $string);
Now you can loop through your data现在您可以遍历您的数据
foreach ($newSting as $key => $value) {
echo $value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.