简介
首先你有一个字符串。 JSON 不是数组、对象或数据结构。 JSON 是一种基于文本的序列化格式——所以是一个花哨的字符串,但仍然只是一个字符串。使用json_decode()在PHP中对其进行解码。
$data = json_decode($json);
你可能会发现:
这些是可以用 JSON 编码的东西。或者更准确地说,这些是可以用 JSON 编码的 PHP 版本。
它们没有什么特别之处。它们不是“JSON 对象”或“JSON 数组”。您已解码 JSON - 您现在拥有 basic everyday PHP types。
对象将是stdClass 的实例,这是一个内置类,它只是一个generic thing,在这里并不重要。
访问对象属性
您访问这些对象之一的properties 的方式与访问任何其他对象的公共非静态属性的方式相同,例如$object->property.
$json = '
{
"type": "donut",
"name": "Cake"
}';
$yummy = json_decode($json);
echo $yummy->type; //donut
访问数组元素
您可以像访问任何其他数组一样访问这些数组之一的元素,例如$array[0].
$json = '
[
"Glazed",
"Chocolate with Sprinkles",
"Maple"
]';
$toppings = json_decode($json);
echo $toppings[1]; //Chocolate with Sprinkles
使用foreach 对其进行迭代。
foreach ($toppings as $topping) {
echo $topping, "\n";
}
釉面
洒巧克力
枫树
或与bazillion built-in array functions 中的任何一个混在一起。
访问嵌套项
对象的属性和数组的元素可能是更多的对象和/或数组 - 您可以像往常一样继续访问它们的属性和成员,例如$object->array[0]->etc.
$json = '
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}';
$yummy = json_decode($json);
echo $yummy->toppings[2]->id; //5004
当你这样做时,你会得到关联数组而不是对象 - 带有字符串的数组作为键。您再次像往常一样访问其中的元素,例如$array['key'].
$json = '
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}';
$yummy = json_decode($json, true);
echo $yummy['toppings'][2]['type']; //Maple
访问关联数组项
在将 JSON object 解码为关联 PHP 数组时,您可以使用 foreach (array_expression as $key => $value) 语法来迭代键和值,例如
$json = '
{
"foo": "foo value",
"bar": "bar value",
"baz": "baz value"
}';
$assoc = json_decode($json, true);
foreach ($assoc as $key => $value) {
echo "The value of key '$key' is '$value'", PHP_EOL;
}
打印
键'foo'的值是'foo value'
键'bar'的值是'bar value'
键'baz'的值是'baz value'
不知道数据的结构
阅读文档以了解您从何处获取 JSON。
查看 JSON - 您看到大括号 {} 期望一个对象,您看到方括号 [] 期望一个数组。
用print_r() 击中解码的数据:
$json = '
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}';
$yummy = json_decode($json);
print_r($yummy);
并检查输出:
stdClass Object
(
[type] => donut
[name] => Cake
[toppings] => Array
(
[0] => stdClass Object
(
[id] => 5002
[type] => Glazed
)
[1] => stdClass Object
(
[id] => 5006
[type] => Chocolate with Sprinkles
)
[2] => stdClass Object
(
[id] => 5004
[type] => Maple
)
)
)
它会告诉你哪里有对象,哪里有数组,以及它们的成员的名称和值。
如果您在迷路之前只能深入其中 - 走那么远并用print_r() 击中那个:
print_r($yummy->toppings[0]);
stdClass Object
(
[id] => 5002
[type] => Glazed
)
去this handy interactive JSON explorer看看吧。
将问题分解成更容易理解的部分。
json_decode() 返回null
发生这种情况是因为:
- JSON 完全由
null 组成。
- JSON 无效 - 检查
json_last_error_msg 的结果或通过类似 JSONLint 的方式输入。
- 它包含嵌套超过 512 层的元素。通过将整数作为第三个参数传递给
json_decode(),可以覆盖默认的最大深度。
如果您需要更改最大深度,您可能解决了错误的问题。找出为什么你会得到如此深度嵌套的数据(例如,你正在查询的生成 JSON 的服务有一个错误)并避免这种情况发生。
对象属性名包含特殊字符
有时您的对象属性名称会包含连字符 - 或符号 @ 之类的内容,但不能在文字标识符中使用。相反,您可以在花括号中使用字符串文字来解决它。
$json = '{"@attributes":{"answer":42}}';
$thing = json_decode($json);
echo $thing->{'@attributes'}->answer; //42
如果您有一个整数作为属性,请参阅:How to access object properties with names like integers? 作为参考。
有人将 JSON 放入你的 JSON
这很荒谬,但它确实发生了 - 在您的 JSON 中将 JSON 编码为字符串。解码,像往常一样访问字符串,解码那个,最终得到你需要的。
$json = '
{
"type": "donut",
"name": "Cake",
"toppings": "[{ \"type\": \"Glazed\" }, { \"type\": \"Maple\" }]"
}';
$yummy = json_decode($json);
$toppings = json_decode($yummy->toppings);
echo $toppings[0]->type; //Glazed
数据不适合内存
如果您的 JSON 太大而无法立即处理 json_decode(),事情就会开始变得棘手。见:
如何排序
请参阅:Reference: all basic ways to sort arrays and data in PHP。