我有以下 JSON 字符串
{
"name":"Product",
"properties":
{
"id":
{
"type":"number",
"description":"Product identifier",
"required":true
},
"name":
{
"type":"string",
"description":"Name of the product",
"required":true
},
"price":
{
"type":"number",
"minimum":0,
"required":true
},
"tags":
{
"type":"array",
"items":
{
"type":"string"
}
},
"stock":
{
"type":"object",
"properties":
{
"warehouse":
{
"type":"number"
},
"retail":
{
"type":"number"
}
}
}
}
}
我想访问
properties - > stock - > properties - > warehouse.
在 python 中,我可以执行以下操作。
f = open("c:/dir/jsondec.json")
data = json.load(f)
node = data['properties']['stock']['properties']['warehouse']
print str(node)
我正在尝试在 PHP 中做同样的事情。我知道我可以使用json_decode()
,但正确的语法应该是什么。另外,如果我在里面有一个数组,properties-> ID
我可以['properties'][0]['id']
访问它。php中的等价物是什么?