介绍
首先,您有一个字符串。JSON不是数组,对象或数据结构。JSON是基于文本的序列化格式,因此是花哨的字符串,但仍然只是字符串。使用解码PHP
json_depre()。
$data = json_depre($json);
在其中您可能会发现:
- 标量:strings,ints,floats和bools
- 空值(其自身的一种特殊类型)
- 复合类型:对象和数组。
这些都是可以用JSON编码的东西。或更准确地说,这些是可以用JSON编码的PHP版本。
他们没有什么特别的。它们不是“ JSON对象”或“ JSON数组”。您已经解码了JSON-现在有了基本的日常PHP类型。
对象将是stdClass的实例,stdClass是一个内置类,这只是一个通用的东西,在这里并不重要。
访问对象属性
您访问的属性的其中之一的对象,你会以同样的方式用于任何其他目的,如公共非静态属性
$object->property。
$json = '{ "type": "donut", "name": "Cake"}';$yummy = json_depre($json);echo $yummy->type; //donut访问数组元素
您可以像访问其他任何数组一样的方式访问这些数组之一的元素
$array[0]。
$json = '[ "Glazed", "Chocolate with Sprinkles", "Maple"]';$toppings = json_depre($json);echo $toppings[1]; //Chocolate with Sprinkles
用遍历它
foreach。
foreach ($toppings as $topping) { echo $topping, "n";}Glazed
Chocolate with Sprinkles
Maple
或与任何成千上万个内置数组函数混为一谈。
访问嵌套项
对象的属性和数组的元素可能是更多的对象和/或数组-您可以像往常一样继续访问它们的属性和成员,例如
$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_depre($json);echo $yummy->toppings[2]->id; //5004true
作为第二个参数传递给json_depre()
当您执行此操作时,您将获得关联数组-带有键字符串的数组,而不是对象。再次按常规方式访问其元素,例如
$array['key']。
$json = '{ "type": "donut", "name": "Cake", "toppings": [ { "id": "5002", "type": "Glazed" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5004", "type": "Maple" } ]}';$yummy = json_depre($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_depre($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_depre($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)
在这个方便的交互式JSON资源管理器中进行查看。
将问题分解为更容易解决的问题。
json_depre()
退货 null
发生这种情况的原因是:
- JSON完全由组成
null
。 - JSON无效-检查结果
json_last_error_msg
或将其放入JSONLint之类的内容。 - 它包含嵌套超过512个级别的元素。可以通过将整数作为第三个参数传递来覆盖此默认的最大深度
json_depre()
。
如果您需要更改最大深度,则可能是在解决错误的问题。找出为什么要获取如此深层的数据(例如,正在查询的生成JSON的服务存在错误),并使其不会发生。
对象属性名称包含特殊字符
有时,您会有一个对象属性名称,其中包含连字符
-或符号
@,但不能在文字标识符中使用。相反,您可以在花括号中使用字符串文字来解决它。
$json = '{"@attributes":{"answer":42}}';$thing = json_depre($json);echo $thing->{'@attributes'}->answer; //42有人将JSON放入JSON
这很荒谬,但是确实发生了-JSON中将JSON编码为字符串。解码,照常访问该字符串, 对该进行 解码,最终得到所需的内容。
$json = '{ "type": "donut", "name": "Cake", "toppings": "[{ "type": "Glazed" }, { "type": "Maple" }]"}';$yummy = json_depre($json);$toppings = json_depre($yummy->toppings);echo $toppings[0]->type; //Glazed数据不适合内存
如果您的JSON太大而
json_depre()无法立即处理,那么事情就会变得棘手。



