[英]How can i access json array in php and save it in .json file
Current PHP Code is working but I need to save JSON as I mentioned in the example I need to access all data of array and save it inside the .JSON File当前的 PHP 代码正在运行,但我需要保存 JSON,正如我在示例中提到的,我需要访问数组的所有数据并将其保存在.JSON文件中
<?php
$curl = curl_init();
$fh = fopen('local.json','w');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, "https://jsonstorage.net/api/items/46b5a62d-9a39-41ab-89f4-e75f458c9189");
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
fwrite($fh, $contents);
curl_close($curl);
fclose($fh);
?>
My JSON data is in this form but I need to access all data of [data]我的 JSON 数据采用这种形式,但我需要访问 [data] 的所有数据
{
"data": [
{
"adapterid": 44835,
"rowid": 1573784208932,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -90,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784212032,
"battery": 3660,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -89,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784215034,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -96,
"temp": 25.75
}
]
}
the response I want is:我想要的回应是:
{
"data": [
**I want this data**
]
}
here is the example sample of my requirement:这是我的要求的示例示例:
[
{
"adapterid": 44835,
"rowid": 1573784208932,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -90,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784212032,
"battery": 3660,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -89,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784215034,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -96,
"temp": 25.75
}
]
How can I access this?我怎样才能访问这个? Thanks in advance提前致谢
Try this:尝试这个:
$contents = json_decode($contents,1);
print_r($contents['data']);
You can turn the json into an associative array using json_decode($contents, true);
您可以使用json_decode($contents, true);
将 json 转换为关联数组json_decode($contents, true);
This allows you to retrieve the data
array inside the json, then turn it back into json using json_encode($contents['data']);
这允许您检索 json 中的data
数组,然后使用json_encode($contents['data']);
to write the data to file:将数据写入文件:
<?php
$curl = curl_init();
$fh = fopen('local.json', 'w');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, "https://jsonstorage.net/api/items/46b5a62d-9a39-41ab-89f4-e75f458c9189");
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
$contents = json_decode($contents, true); // to associative array
$contents = json_encode($contents['data']); // to json, only the 'data' array
fwrite($fh, $contents); // write json 'data' array to file
curl_close($curl);
fclose($fh);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.