This is intended to be a general reference question and answer covering many of the never-ending "How do I access data in my JSON?" questions. It is here to handle the broad basics of decoding JSON in PHP and accessing the results.
这是一个通用的参考问题和答案,涵盖了许多没完没了的“如何在JSON中访问数据?”它在这里处理PHP中解码JSON和访问结果的基本知识。
I have the JSON:
我有JSON:
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}
How do I decode this in PHP and access the resulting data?
如何在PHP中解码并访问结果数据?
2 个解决方案
#1
285
Intro
First off you have a string. JSON is not an array, an object, or a data structure. JSON is a text-based serialization format - so a fancy string, but still just a string. Decode it in PHP by using json_decode()
.
首先你有一根绳子。JSON不是数组、对象或数据结构。JSON是一种基于文本的序列化格式——它是一种奇特的字符串,但仍然只是一个字符串。使用json_decode()在PHP中解码它。
$data = json_decode($json);
Therein you might find:
在那里你会发现:
- scalars: strings, ints, floats, and bools
- scalars:字符串、int、float和bools
- nulls (a special type of its own)
- nulls(本身的一种特殊类型)
- compound types: objects and arrays.
- 复合类型:对象和数组。
These are the things that can be encoded in JSON. Or more accurately, these are PHP's versions of the things that can be encoded in JSON.
这些都是可以用JSON编码的东西。更准确地说,这些是可以用JSON编码的PHP版本。
There's nothing special about them. They are not "JSON objects" or "JSON arrays." You've decoded the JSON - you now have basic everyday PHP types.
它们没什么特别的。它们不是“JSON对象”或“JSON数组”。您已经解码了JSON——现在您有了基本的日常PHP类型。
Objects will be instances of stdClass, a built-in class which is just a generic thing that's not important here.
对象将是stdClass的实例,一个内置类,它只是一个普通的东西,在这里并不重要。
Accessing object properties
You access the properties of one of these objects the same way you would for the public non-static properties of any other object, e.g. $object->property
.
您访问其中一个对象的属性的方式与访问任何其他对象的公共非静态属性的方式相同,例如$object->属性。
$json = '
{
"type": "donut",
"name": "Cake"
}';
$yummy = json_decode($json);
echo $yummy->type; //donut
Accessing array elements
You access the elements of one of these arrays the same way you would for any other array, e.g. $array[0]
.
您访问这些数组中的一个元素的方式与访问其他数组的方式相同,例如$array[0]。
$json = '
[
"Glazed",
"Chocolate with Sprinkles",
"Maple"
]';
$toppings = json_decode($json);
echo $toppings[1]; //Chocolate with Sprinkles
Iterate over it with foreach
.
用foreach迭代它。
foreach ($toppings as $topping) {
echo $topping, "\n";
}
Glazed
Chocolate with Sprinkles
Maple糖巧克力加糖枫糖
Or mess about with any of the bazillion built-in array functions.
或者使用大量的内置数组函数。
Accessing nested items
The properties of objects and the elements of arrays might be more objects and/or arrays - you can simply continue to access their properties and members as usual, e.g. $object->array[0]->etc
.
对象的属性和数组的元素可能是更多的对象和/或数组——您可以简单地继续访问它们的属性和成员,例如$object->数组[0]->等等。
$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
Passing true
as the second argument to json_decode()
When you do this, instead of objects you'll get associative arrays - arrays with strings for keys. Again you access the elements thereof as usual, e.g. $array['key']
.
当你这样做的时候,你会得到关联数组——数组和字符串作为键。同样,您可以像往常一样访问它的元素,例如$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
Don't know how the data is structured
Read the documentation for whatever it is you're getting the JSON from.
阅读文档,了解JSON的来源。
Look at the JSON - where you see curly brackets {}
expect an object, where you see square brackets []
expect an array.
看看JSON——你会看到花括号{}期望对象,方括号[]期望数组。
Hit the decoded data with a print_r()
:
使用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);
and check the output:
并检查输出:
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
)
)
)
It'll tell you where you have objects, where you have arrays, along with the names and values of their members.
它会告诉你哪里有对象,哪里有数组,以及它们的成员的名称和值。
If you can only get so far into it before you get lost - go that far and hit that with print_r()
:
如果你在迷路之前只能走到这一步,那就用print_r():
print_r($yummy->toppings[0]);
stdClass Object
(
[id] => 5002
[type] => Glazed
)
Break the problem down into pieces that are easier to wrap your head around.
把问题分解成更容易理解的部分。
json_decode()
returns null
This happens because either:
这是因为:
- The JSON consists entirely of just that,
null
. - JSON完全是这样的,null。
- The JSON is invalid - check the result of
json_last_error_msg
or put it through something like JSONLint. - JSON是无效的——检查json_last_error_msg的结果,或者通过JSONLint之类的方法进行处理。
- It contains elements nested more than 512 levels deep. This default max depth can be overridden by passing an integer as the third argument to
json_decode()
. - 它包含嵌套超过512层的元素。通过将一个整数作为json_decode()的第三个参数传递给它,可以覆盖这个默认的最大深度。
If you need to change the max depth you're probably solving the wrong problem. Find out why you're getting such deeply nested data (e.g. the service you're querying that's generating the JSON has a bug) and get that to not happen.
如果你需要改变最大深度,你可能解决了错误的问题。找出为什么要获取如此深度嵌套的数据(例如,生成JSON的服务有问题),并避免这种情况发生。
Object property name contains a special character
Sometimes you'll have an object property name that contains something like a hyphen -
or at sign @
which can't be used in a literal identifier. Instead you can use a string literal within curly braces to address it.
有时,您会有一个对象属性名,其中包含连字符——或@ @符号,在文字标识符中不能使用。相反,您可以在花括号内使用字符串文字来处理它。
$json = '{"@attributes":{"answer":42}}';
$thing = json_decode($json);
echo $thing->{'@attributes'}->answer; //42
If you have an integer as property see: How to access object properties with names like integers? as reference.
如果您有一个整数作为属性,请参见:如何使用像整数这样的名称访问对象属性?作为参考。
Someone put JSON in your JSON
It's ridiculous but it happens - there's JSON encoded as a string within your JSON. Decode, access the string as usual, decode that, and eventually get to what you need.
这很荒谬,但它确实发生了——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
Data doesn't fit in memory
If your JSON is too large for json_decode()
to handle at once things start to get tricky. See:
如果JSON太大,json_decode()无法立即处理,事情就会变得棘手起来。看到的:
- Processing large JSON files in PHP
- 在PHP中处理大型JSON文件
- How to properly iterate through a big json file
- 如何正确地遍历大型json文件
How to sort it
See: Reference: all basic ways to sort arrays and data in PHP.
参见:Reference:在PHP中排序数组和数据的所有基本方法。
#2
3
You can use json_decode() to convert a json string to a PHP object/array.
可以使用json_decode()将json字符串转换为PHP对象/数组。
Eg.
如。
Input:
输入:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
Output:
输出:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
Few Points to remember:
要记住几点:
-
json_decode
requires the string to be a validjson
else it will returnNULL
. - json_decode要求字符串是有效的json,否则它会返回NULL。
- In the event of a failure to decode,
json_last_error()
can be used to determine the exact nature of the error. - 如果解码失败,可以使用json_last_error()来确定错误的确切性质。
- Make sure you pass in
utf8
content, orjson_decode
may error out and just return aNULL
value. - 确保您传递了utf8内容,否则json_decode可能会出错,只返回一个空值。
#1
285
Intro
First off you have a string. JSON is not an array, an object, or a data structure. JSON is a text-based serialization format - so a fancy string, but still just a string. Decode it in PHP by using json_decode()
.
首先你有一根绳子。JSON不是数组、对象或数据结构。JSON是一种基于文本的序列化格式——它是一种奇特的字符串,但仍然只是一个字符串。使用json_decode()在PHP中解码它。
$data = json_decode($json);
Therein you might find:
在那里你会发现:
- scalars: strings, ints, floats, and bools
- scalars:字符串、int、float和bools
- nulls (a special type of its own)
- nulls(本身的一种特殊类型)
- compound types: objects and arrays.
- 复合类型:对象和数组。
These are the things that can be encoded in JSON. Or more accurately, these are PHP's versions of the things that can be encoded in JSON.
这些都是可以用JSON编码的东西。更准确地说,这些是可以用JSON编码的PHP版本。
There's nothing special about them. They are not "JSON objects" or "JSON arrays." You've decoded the JSON - you now have basic everyday PHP types.
它们没什么特别的。它们不是“JSON对象”或“JSON数组”。您已经解码了JSON——现在您有了基本的日常PHP类型。
Objects will be instances of stdClass, a built-in class which is just a generic thing that's not important here.
对象将是stdClass的实例,一个内置类,它只是一个普通的东西,在这里并不重要。
Accessing object properties
You access the properties of one of these objects the same way you would for the public non-static properties of any other object, e.g. $object->property
.
您访问其中一个对象的属性的方式与访问任何其他对象的公共非静态属性的方式相同,例如$object->属性。
$json = '
{
"type": "donut",
"name": "Cake"
}';
$yummy = json_decode($json);
echo $yummy->type; //donut
Accessing array elements
You access the elements of one of these arrays the same way you would for any other array, e.g. $array[0]
.
您访问这些数组中的一个元素的方式与访问其他数组的方式相同,例如$array[0]。
$json = '
[
"Glazed",
"Chocolate with Sprinkles",
"Maple"
]';
$toppings = json_decode($json);
echo $toppings[1]; //Chocolate with Sprinkles
Iterate over it with foreach
.
用foreach迭代它。
foreach ($toppings as $topping) {
echo $topping, "\n";
}
Glazed
Chocolate with Sprinkles
Maple糖巧克力加糖枫糖
Or mess about with any of the bazillion built-in array functions.
或者使用大量的内置数组函数。
Accessing nested items
The properties of objects and the elements of arrays might be more objects and/or arrays - you can simply continue to access their properties and members as usual, e.g. $object->array[0]->etc
.
对象的属性和数组的元素可能是更多的对象和/或数组——您可以简单地继续访问它们的属性和成员,例如$object->数组[0]->等等。
$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
Passing true
as the second argument to json_decode()
When you do this, instead of objects you'll get associative arrays - arrays with strings for keys. Again you access the elements thereof as usual, e.g. $array['key']
.
当你这样做的时候,你会得到关联数组——数组和字符串作为键。同样,您可以像往常一样访问它的元素,例如$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
Don't know how the data is structured
Read the documentation for whatever it is you're getting the JSON from.
阅读文档,了解JSON的来源。
Look at the JSON - where you see curly brackets {}
expect an object, where you see square brackets []
expect an array.
看看JSON——你会看到花括号{}期望对象,方括号[]期望数组。
Hit the decoded data with a print_r()
:
使用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);
and check the output:
并检查输出:
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
)
)
)
It'll tell you where you have objects, where you have arrays, along with the names and values of their members.
它会告诉你哪里有对象,哪里有数组,以及它们的成员的名称和值。
If you can only get so far into it before you get lost - go that far and hit that with print_r()
:
如果你在迷路之前只能走到这一步,那就用print_r():
print_r($yummy->toppings[0]);
stdClass Object
(
[id] => 5002
[type] => Glazed
)
Break the problem down into pieces that are easier to wrap your head around.
把问题分解成更容易理解的部分。
json_decode()
returns null
This happens because either:
这是因为:
- The JSON consists entirely of just that,
null
. - JSON完全是这样的,null。
- The JSON is invalid - check the result of
json_last_error_msg
or put it through something like JSONLint. - JSON是无效的——检查json_last_error_msg的结果,或者通过JSONLint之类的方法进行处理。
- It contains elements nested more than 512 levels deep. This default max depth can be overridden by passing an integer as the third argument to
json_decode()
. - 它包含嵌套超过512层的元素。通过将一个整数作为json_decode()的第三个参数传递给它,可以覆盖这个默认的最大深度。
If you need to change the max depth you're probably solving the wrong problem. Find out why you're getting such deeply nested data (e.g. the service you're querying that's generating the JSON has a bug) and get that to not happen.
如果你需要改变最大深度,你可能解决了错误的问题。找出为什么要获取如此深度嵌套的数据(例如,生成JSON的服务有问题),并避免这种情况发生。
Object property name contains a special character
Sometimes you'll have an object property name that contains something like a hyphen -
or at sign @
which can't be used in a literal identifier. Instead you can use a string literal within curly braces to address it.
有时,您会有一个对象属性名,其中包含连字符——或@ @符号,在文字标识符中不能使用。相反,您可以在花括号内使用字符串文字来处理它。
$json = '{"@attributes":{"answer":42}}';
$thing = json_decode($json);
echo $thing->{'@attributes'}->answer; //42
If you have an integer as property see: How to access object properties with names like integers? as reference.
如果您有一个整数作为属性,请参见:如何使用像整数这样的名称访问对象属性?作为参考。
Someone put JSON in your JSON
It's ridiculous but it happens - there's JSON encoded as a string within your JSON. Decode, access the string as usual, decode that, and eventually get to what you need.
这很荒谬,但它确实发生了——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
Data doesn't fit in memory
If your JSON is too large for json_decode()
to handle at once things start to get tricky. See:
如果JSON太大,json_decode()无法立即处理,事情就会变得棘手起来。看到的:
- Processing large JSON files in PHP
- 在PHP中处理大型JSON文件
- How to properly iterate through a big json file
- 如何正确地遍历大型json文件
How to sort it
See: Reference: all basic ways to sort arrays and data in PHP.
参见:Reference:在PHP中排序数组和数据的所有基本方法。
#2
3
You can use json_decode() to convert a json string to a PHP object/array.
可以使用json_decode()将json字符串转换为PHP对象/数组。
Eg.
如。
Input:
输入:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
Output:
输出:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
Few Points to remember:
要记住几点:
-
json_decode
requires the string to be a validjson
else it will returnNULL
. - json_decode要求字符串是有效的json,否则它会返回NULL。
- In the event of a failure to decode,
json_last_error()
can be used to determine the exact nature of the error. - 如果解码失败,可以使用json_last_error()来确定错误的确切性质。
- Make sure you pass in
utf8
content, orjson_decode
may error out and just return aNULL
value. - 确保您传递了utf8内容,否则json_decode可能会出错,只返回一个空值。