[英]Decode json data from .json file in php
I am trying to output some json data from a json file via php but it does not seem to work. 我正在尝试通过php从json文件输出一些json数据,但它似乎不起作用。 I tried this:
我尝试了这个:
<?php
$jsonFile = file_get_contents('dataset/dataset.json');
$data = json_decode($jsonFile, true);
echo $data->{'data'}[0]->{'letter'}
?>
The json file is following: json文件如下:
{
"data":[
{
"letter":"A",
"blocks":{
"1":"0",
"2":"0",
"3":"0",
"4":"0",
"5":"0"
}
}
]}
Basically it should output the letter "A" but it outputs nothing. 基本上,它应该输出字母“ A”,但什么都不输出。 What did I do wrong?
我做错了什么? Thanks
谢谢
PS I tried to do it like here: How to process JSON in PHP? PS我试图这样做,如: 如何在PHP中处理JSON? but it does not work.
但它不起作用。
After json_decode($jsonFile, true)
your data is in array. 在
json_decode($jsonFile, true)
您的数据在数组中。 So you should not access using object. 因此,您不应使用object访问。 Access data by array index.
通过数组索引访问数据。 Try this..
尝试这个..
echo $data['data'][0]['letter'];
This says, you get an array (the true parameter): 这就是说,您得到一个数组(true参数):
$data = json_decode($jsonFile, true);
You can see this if you do this: 如果执行此操作,则可以看到以下内容:
print_r($data);
Try this: 尝试这个:
echo $data['data'][0]['letter'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.