php获取到的json数据如何处理_如何使用PHP从JSON提取数据?

小编典典

介绍

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

$data = json_decode($json);

在其中您可能会发现:

标量:strings,ints,floats和bools

空值(其自身的一种特殊类型)

复合类型:对象和数组。

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

他们没有什么特别的。它们不是“ JSON对象”或“ JSON数组”。您已经解码了JSON-现在有了基本的日常PHP类型。

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

访问对象属性

您访问的属性的其中之一的对象,你会以同样的方式用于任何其他目的,如公共非静态属性$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";

}

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_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

)

在这个方便的交互式JSON资源管理器中进行查看。

将问题分解为更容易解决的问题。

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

有人将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()无法立即处理,那么事情就会变得棘手。

2020-05-26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值