如何使用PHP从JSON中提取数据?
这是一个一般性的参考问题和答案,涵盖了许多永无休止的“如何访问JSON中的数据?”问题。它在这里处理在PHP中解码JSON和访问结果的广泛基础知识 我有JSON:如何使用PHP从JSON中提取数据?,php,json,Php,Json,这是一个一般性的参考问题和答案,涵盖了许多永无休止的“如何访问JSON中的数据?”问题。它在这里处理在PHP中解码JSON和访问结果的广泛基础知识 我有JSON: { "type": "donut", "name": "Cake", "toppings": [ { "id": "5002", "type": "Glazed" }, { "id": "5006", "type": "Chocolate with Sprinkles" },
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}
如何在PHP中解码并访问结果数据?Intro
首先,你有一根绳子。JSON不是数组、对象或数据结构。是一种基于文本的序列化格式,所以是一个奇特的字符串,但仍然只是一个字符串。使用PHP对其进行解码
你可能会发现:
- 标量:、和
- (自身的一种特殊类型)
- 复合类型:和
访问对象属性 访问其中一个对象的方法与访问任何其他对象的公共非静态属性的方法相同,例如
$object->property
$json = '
{
"type": "donut",
"name": "Cake"
}';
$yummy = json_decode($json);
echo $yummy->type; //donut
访问数组元素 访问其中一个数组的元素的方式与访问任何其他数组的方式相同,例如 重复使用 光滑的
巧克力撒糖
枫树 或者和任何一个女人乱搞
访问嵌套项 对象的属性和数组的元素可能是更多的对象和/或数组-您可以像往常一样继续访问它们的属性和成员,例如
$object->array[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
将true
作为第二个参数传递给
执行此操作时,您将获得关联数组,而不是对象-带有键字符串的数组。同样,您可以像往常一样访问其中的元素,例如$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数组时,可以使用以下语法迭代键和值,例如
$json=”
{
“foo”:“foo值”,
“条”:“条值”,
“baz”:“baz值”
}';
$assoc=json_decode($json,true);
foreach($assoc as$key=>$value){
echo“key'$key'的值是'$value'”,PHP_EOL;
}
印刷品
键“foo”的值为“foo值”
键“bar”的值是“bar value”
键“baz”的值为“baz值”
不知道数据是如何构造的
阅读文档,了解JSON的来源
看看JSON——在这里,你可以看到花括号{}
中包含一个对象,在这里,你可以看到方括号[]
中包含一个数组
使用以下命令点击解码数据:
并检查输出:
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()
:
请仔细看一下
把问题分解成更容易理解的部分
json\u decode()
返回null
这是因为:
JSON完全由以下内容组成:null
JSON无效-请检查结果或执行类似的操作
它包含嵌套深度超过512层的元素。通过将整数作为第三个参数传递给,可以覆盖此默认最大深度
如果需要更改最大深度,则可能是解决了错误的问题。找出为什么您会得到如此深的嵌套数据(例如,您正在查询的生成JSON的服务有一个bug),并避免这种情况发生
对象属性名称包含特殊字符
有时,对象属性名包含连字符-
或符号@
,不能在文字标识符中使用。相反,您可以在大括号内使用字符串文字来处理它
$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\u decode()
无法立即处理,那么事情就会变得棘手。见:
如何分类
请参阅:。您可以使用它将json字符串转换为PHP对象/数组
例如
输入:
$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 = '{"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\u decode
要求字符串为有效的json
,否则它将返回NULL
- 在解码失败的情况下,可用于确定错误的确切性质
- 确保传入
utf8
内容,或者json\u decode
可能会出错,只返回一个NULL
值
检查下面的代码,以便在PHP
中将json转换为数组,
如果JSON是正确的,那么JSON\u decode()
工作正常,并将返回一个数组,
但是如果JSON格式不正确,那么它将返回NULL
<?php
function jsonDecode1($json){
$arr = json_decode($json, true);
return $arr;
}
// In case of malformed JSON, it will return NULL
var_dump( jsonDecode1($json) );
我们可以使用php中的json_解码函数将json字符串解码到数组中
1) json\u decode($json\u string)//它返回对象
2) json\u decode($json\u string,true)//返回数组
$json_string = '{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}';
$array = json_decode($json_string,true);
echo $array['type']; //it gives donut
//使用json作为php数组
$json='[{“用户id”:“1”,“用户名”:“Sayeed Amin”,“时间”:“2019-11-06 13:21:26”}];
//或从文件中使用
//$json=file_get_contents('results.json');
$someArray=json_decode($json,true);
foreach($someArray作为$key=>$value){
echo$value[“使用
$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)
}
<?php
function jsonDecode1($json){
$arr = json_decode($json, true);
return $arr;
}
// In case of malformed JSON, it will return NULL
var_dump( jsonDecode1($json) );
<?php
function jsonDecode2($json){
$arr = (array) json_decode($json, true);
return $arr;
}
// In case of malformed JSON, it will return an empty array()
var_dump( jsonDecode2($json) );
<?php
function jsonDecode3($json){
$arr = (array) json_decode($json, true);
if(empty(json_last_error())){
return $arr;
}
else{
throw new ErrorException( json_last_error_msg() );
}
}
// In case of malformed JSON, Fatal error will be generated
var_dump( jsonDecode3($json) );
$json_string = '{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}';
$array = json_decode($json_string,true);
echo $array['type']; //it gives donut
// Using json as php array
$json = '[{"user_id":"1","user_name":"Sayeed Amin","time":"2019-11-06 13:21:26"}]';
//or use from file
//$json = file_get_contents('results.json');
$someArray = json_decode($json, true);
foreach ($someArray as $key => $value) {
echo $value["user_id"] . ", " . $value["user_name"] . ", " . $value["time"] . "<br>";
}
function load_utf8_file($filePath)
{
$response = null;
try
{
if (file_exists($filePath)) {
$text = file_get_contents($filePath);
$response = preg_replace("/^\xEF\xBB\xBF/", '', $text);
}
} catch (Exception $e) {
echo 'ERROR: ', $e->getMessage(), "\n";
}
finally{ }
return $response;
}
$str = load_utf8_file('appconfig.json');
$json = json_decode($str, true);
//print_r($json);
echo $json['prod']['deploy']['hostname'];