【发布时间】:2016-07-28 10:57:31
【问题描述】:
我正在尝试解码以下 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 数组中每个对象的属性
我正在针对此架构验证 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)
但我仍然遇到同样的错误
【问题讨论】:
-
据我所知,你不能
echo一个数组。 -
对于此代码,您将获得“注意:数组到字符串的转换”。所以你的错误来自其他地方。
标签: php arrays json jsonschema