PHP json_decode error: Array value found, but an object is required
PHP json_decode error: Array value found, but an object is required
我正在尝试解码以下 JSON 文件
{
"email": "mail@gmail.com",
"password": "12345",
"languageProficiency": {
"language": "English",
"proficiency": 4
},
"tags": [
{
"name": "singing"
},
{
"name": "dance"
}
]
}
当我这样做时
$data = json_decode($jsonContent, true);
echo $data;
die();
我有以下错误:
找到数组值,但需要一个对象
问题
1) 如何查看来自 JSON 的数据
2) 如何访问 tags array
中每个对象的 属性
我正在根据此模式验证 Json 内容
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"type": "string"
},
"languageProficiency": {
"type": "object",
"properties": {
"language": {
"type": "string"
},
"proficiency": {
"type": "integer"
}
},
"required": [
"language",
"proficiency"
]
},
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
}
},
"required": [
"email",
"password",
"languageProficiency",
"tags"
]
}
更新
我尝试关注查看json内容
print_r($data)
但我仍然得到同样的错误
$data = json_decode($jsonContent, true);
上面这行代码会return数组,不能直接用echo打印数组。
要打印数组的特定值(例如电子邮件),请这样做:
echo $data["email"];
提示:
用print_r()知道数组结构是这样的,
echo "<pre>";
print_r($data);
您可以尝试 print_r($data) 来探索价值
我正在尝试解码以下 JSON 文件
{
"email": "mail@gmail.com",
"password": "12345",
"languageProficiency": {
"language": "English",
"proficiency": 4
},
"tags": [
{
"name": "singing"
},
{
"name": "dance"
}
]
}
当我这样做时
$data = json_decode($jsonContent, true);
echo $data;
die();
我有以下错误:
找到数组值,但需要一个对象
问题
1) 如何查看来自 JSON 的数据 2) 如何访问 tags array
中每个对象的 属性我正在根据此模式验证 Json 内容
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"type": "string"
},
"languageProficiency": {
"type": "object",
"properties": {
"language": {
"type": "string"
},
"proficiency": {
"type": "integer"
}
},
"required": [
"language",
"proficiency"
]
},
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
}
},
"required": [
"email",
"password",
"languageProficiency",
"tags"
]
}
更新
我尝试关注查看json内容
print_r($data)
但我仍然得到同样的错误
$data = json_decode($jsonContent, true);
上面这行代码会return数组,不能直接用echo打印数组。
要打印数组的特定值(例如电子邮件),请这样做:
echo $data["email"];
提示:
用print_r()知道数组结构是这样的,
echo "<pre>";
print_r($data);
您可以尝试 print_r($data) 来探索价值