如何将 JSON 字符串转换为数组 [英] How to convert JSON string to array
本文介绍了如何将 JSON 字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的是:
- 将 JSON 作为来自 php 文本区域的输入
- 使用此输入并将其转换为 JSON 并将其传递给 php curl 以发送请求.
这个从 api 获取这个 json 字符串的 php 我想传递给 json 但它没有转换为数组
this m getting at php from get of api this json string i want to pass to json but it is not converting to array
echo $str='{
action : "create",
record: {
type: "n$product",
fields: {
n$name: "Bread",
n$price: 2.11
},
namespaces: { "my.demo": "n" }
}
}';
$json = json_decode($str, true);
上面的代码没有返回数组.
the above code is not returning me array.
推荐答案
如果您将帖子中的 JSON 传递给 json_decode
,它将失败.有效的 JSON 字符串包含带引号的键:
If you pass the JSON in your post to json_decode
, it will fail. Valid JSON strings have quoted keys:
json_decode('{foo:"bar"}'); // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}'); // returns an object, not an array.
这篇关于如何将 JSON 字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文