gpt4 book ai didi

php - 如何使用PHP从JSON提取数据?

转载 作者:行者123 更新时间:2023-11-29 16:14:22 24 4
gpt4 key购买 nike

这旨在作为一般参考问题和答案,涵盖许多永无止境的“如何访问JSON中的数据?”问题。它是在这里处理在PHP中解码JSON和访问结果的广泛基础知识。


我有JSON:

{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}


如何在PHP中对此进行解码并访问结果数据?

最佳答案

介绍

首先,你有一个字符串。 JSON不是数组,对象或数据结构。 JSON是基于文本的序列化格式,因此是花哨的字符串,但仍然只是字符串。使用json_decode()在PHP中对其进行解码。

 $data = json_decode($json);


在其中您可能会发现:


标量: stringsintsfloatsbools
nulls(一种特殊的类型)
复合类型: objectsarrays


这些都是可以用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




true作为第二个参数传递给 json_decode()

当您执行此操作时,将获得关联数组-带有键字符串的数组,而不是对象。再次您照常访问其元素,例如 $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对象解码为关联的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()无法立即处理,那么事情就会变得棘手。看到:


Processing large JSON files in PHP
How to properly iterate through a big json file




如何排序

请参阅: Reference: all basic ways to sort arrays and data in PHP

关于php - 如何使用PHP从JSON提取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54960434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com