我如何从PHP中提取JSON数据?

这是一个普遍的参考问题和答案,涵盖了许多永无止境的“我如何在我的JSON中访问数据?” 的问题。 这里是处理在PHP中解码JSON的基本知识,并访问结果。

我有JSON:

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

我如何解码这在PHP中,并访问结果数据?

介绍

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

  $data = json_decode($json); 

其中你可能会发现:

  • 标量: string , 整数 , 浮点数和布尔值
  • 空值 (它自己的特殊types)
  • 复合types: 对象和数组 。

这些是可以用JSON编码的东西。 或者更准确地说,这些是可以用JSON编码的东西的PHP版本。

他们没有什么特别的。 它们不是“JSON对象”或“JSON数组”。 你已经解码了JSON – 你现在有基本的每日PHPtypes 。

对象将是stdClass的实例,这是一个内置的类,这只是一个通用的东西 ,在这里不重要。


访问对象属性

您可以像访问任何其他对象的公共非静态属性(例如$object->property一样访问这些对象之一的$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内置的数组函数混淆 。


访问嵌套的项目

对象的属性和数组的元素可能是更多的对象和/或数组 – 你可以像往常一样继续访问它们的属性和成员,例如$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 

作为json_decode()的第二个parameter passingtrue

当你这样做,而不是对象,你会得到关联数组 – 键与string的数组。 再次像往常一样访问其元素,例如$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 

不知道数据是如何构build的

阅读文档,无论您从中获取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()

 print_r($yummy->toppings[0]); 
 stdClass Object ( [id] => 5002 [type] => Glazed ) 

将问题分解成更容易包裹头部的碎片。


json_decode()返回null

发生这种情况是因为:

  1. JSON完全由null
  2. JSON是无效的 – 检查json_last_error_msg的结果或通过像JSONLint这样的东西 。
  3. 它包含嵌套超过512个层次的元素。 这个默认的最大深度可以通过传递一个整数作为json_decode()的第三个参数来json_decode()

如果你需要改变最大深度,你可能会解决错误的问题。 找出为什么你得到这样的深层嵌套的数据(例如,你正在查询的服务,这是产生JSON有一个错误),并得到这一切不会发生。


对象属性名称包含一个特殊字符

有时你会有一个对象属性名称,其中包含像连字符-或在符号@不能用于字面标识符。 相反,您可以使用大括号内的string来解决这个问题。

 $json = '{"@attributes":{"answer":42}}'; $thing = json_decode($json); echo $thing->{'@attributes'}->answer; //42 

如果你有一个整数属性见: 如何访问像整数名称的对象属性? 作为参考。


有人把JSON放入你的JSON中

这是荒谬的,但它发生 – 有JSON编码为您的JSON中的string。 解码,像往常一样访问string,解码,并最终得到你所需要的。

 $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()来说过于庞大, json_decode()一旦事情开始变得棘手。 看到:

  • 在PHP中处理大型的JSON文件
  • 如何正确地遍历一个大的JSON文件

如何sorting

请参阅: 参考:在PHP中对数组和数据进行sorting的所有基本方法 。

您可以使用json_decode()将jsonstring转换为PHP对象/数组。

例如。

input:

 $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true)); 

输出:

 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) } 

要记住的几点:

  • json_decode要求string是一个有效的json否则它将返回NULL
  • 如果解码失败,可​​以使用json_last_error()来确定错误的确切性质。
  • 确保你传入了utf8内容,否则json_decode可能会出错,只是返回NULL值。