通过 PHP 访问 JSON 中的数据
Access data in JSON via PHP
我正在尝试从数据库访问通过目录服务生成的数据,创建 ics 文件,这些文件通过 JSON 编码发送到网站。
正在生成多个文件或数量未知。此时我的主要问题是,我需要将 JSON 文件中的信息解析回 ics 文件。
我现在坚持的是阅读 JSON 文件中的信息。
JSON 文件是这样的:
{
"SMSDATA": {
"date": 20170817062448,
"person": {
"data":[{
"person": 321654978,
"information": "person information"
},{
"person": 3216549,
"information": "person information"
}]
}}
我想做的是深入挖掘并将 person
和 information
数据输出到 ics 文件中。
我目前在 PHP 中仅显示数据以查看我是否正在访问正确数据的是:
$str = '../filename/test.json'; // The location of the files
$contents = file_get_contents($str); // get the information from the file
$decode = json_decode($contents, true); // Creates the JSON as an array
//array loop
foreach($decode as $key => $value){
echo $value["person"]. " -> " . $value["information"]. "<br>"; // Should display the information needed
print_r ($value); // test dump of data
}
我很清楚我挖掘得不够深入。我发现这个资源非常有用:, as well as Convert and Loop through JSON with PHP and JavaScript Arrays/Objects. I also believe that ICal Parser 会有很大用处,但不确定在这个阶段如何使用它。
我只想确保我现在正在读取正确的数据,然后下一步将创建包含人员和信息数据的 .ics 文件。
谢谢大家的帮助。
(顺便说一句,我只使用 PHP 几个月)
你是对的,你没有在正确的数组成员上开始循环。
$contents = '{
"SMSDATA": {
"date": 20170817062448,
"person": {
"data":[{
"person": 321654978,
"information": "person information"
},{
"person": 3216549,
"information": "person information"
}]
}
}
}';
$decode = json_decode($contents, true);
print_r($decode);
foreach($decode['SMSDATA']['person']['data'] as $value){
echo $value["person"]. " -> " . $value["information"]. "<br>";
}
结果
321654978 -> person information<br>
3216549 -> person information<br>
首先,您的 json 格式有误。缺少一个支架。
这里是正确的json:
{
"SMSDATA": {
"date": 20170817062448,
"person": {
"data": [{
"person": 321654978,
"information": "person information"
}, {
"person": 3216549,
"information": "person information"
}]
}
}
}
而对于 php 文件,您需要遍历数组,但由于它是一个多维数组,因此需要多个 foreach。我还添加了 pre 以正确显示它返回的数组,这样您就可以更轻松地查看数组的外观。这是 php 文件:
<?php
$str = 'test.json'; // The location of the files
$contents = file_get_contents($str); // get the information from the file
$decode = json_decode($contents, true); // Creates the JSON as an array
echo "<pre>".print_r($decode, true)."</pre>";
foreach ($decode as $key => $value) {
echo $value["date"]."<br>";
foreach ($value["person"] as $k => $val) {
foreach ($val as $index => $data) {
echo $data["person"]."<br>";
echo $data["information"]."<br>";
}
}
}
?>
我正在尝试从数据库访问通过目录服务生成的数据,创建 ics 文件,这些文件通过 JSON 编码发送到网站。
正在生成多个文件或数量未知。此时我的主要问题是,我需要将 JSON 文件中的信息解析回 ics 文件。
我现在坚持的是阅读 JSON 文件中的信息。
JSON 文件是这样的:
{
"SMSDATA": {
"date": 20170817062448,
"person": {
"data":[{
"person": 321654978,
"information": "person information"
},{
"person": 3216549,
"information": "person information"
}]
}}
我想做的是深入挖掘并将 person
和 information
数据输出到 ics 文件中。
我目前在 PHP 中仅显示数据以查看我是否正在访问正确数据的是:
$str = '../filename/test.json'; // The location of the files
$contents = file_get_contents($str); // get the information from the file
$decode = json_decode($contents, true); // Creates the JSON as an array
//array loop
foreach($decode as $key => $value){
echo $value["person"]. " -> " . $value["information"]. "<br>"; // Should display the information needed
print_r ($value); // test dump of data
}
我很清楚我挖掘得不够深入。我发现这个资源非常有用:
我只想确保我现在正在读取正确的数据,然后下一步将创建包含人员和信息数据的 .ics 文件。
谢谢大家的帮助。
(顺便说一句,我只使用 PHP 几个月)
你是对的,你没有在正确的数组成员上开始循环。
$contents = '{
"SMSDATA": {
"date": 20170817062448,
"person": {
"data":[{
"person": 321654978,
"information": "person information"
},{
"person": 3216549,
"information": "person information"
}]
}
}
}';
$decode = json_decode($contents, true);
print_r($decode);
foreach($decode['SMSDATA']['person']['data'] as $value){
echo $value["person"]. " -> " . $value["information"]. "<br>";
}
结果
321654978 -> person information<br>
3216549 -> person information<br>
首先,您的 json 格式有误。缺少一个支架。
这里是正确的json:
{
"SMSDATA": {
"date": 20170817062448,
"person": {
"data": [{
"person": 321654978,
"information": "person information"
}, {
"person": 3216549,
"information": "person information"
}]
}
}
}
而对于 php 文件,您需要遍历数组,但由于它是一个多维数组,因此需要多个 foreach。我还添加了 pre 以正确显示它返回的数组,这样您就可以更轻松地查看数组的外观。这是 php 文件:
<?php
$str = 'test.json'; // The location of the files
$contents = file_get_contents($str); // get the information from the file
$decode = json_decode($contents, true); // Creates the JSON as an array
echo "<pre>".print_r($decode, true)."</pre>";
foreach ($decode as $key => $value) {
echo $value["date"]."<br>";
foreach ($value["person"] as $k => $val) {
foreach ($val as $index => $data) {
echo $data["person"]."<br>";
echo $data["information"]."<br>";
}
}
}
?>