gpt4 book ai didi

php - 将 JSON 文件解析为 PHP 中的数组

转载 作者:行者123 更新时间:2023-12-02 15:12:52 25 4
gpt4 key购买 nike

我目前正在尝试从 JSON 文件中读取坐标并将其保存到数组中以供进一步使用。但是我没有设法成功读取文件。我的 JSON 文件具有以下格式(我只关心 cX 和 cY):

{
"cX": [
246,
1253,
1464,
1183
],
"cY": [
223,
138,
383,
114
],
"scroll": [
0,
90,
0,
0
],
"sessionID": [
"f06807c10fb31d5530ad8c4236b94ee2",
"f06807c10fb31d5530ad8c4236b94ee2",
"f06807c10fb31d5530ad8c4236b94ee2",
"f06807c10fb31d5530ad8c4236b94ee2"
],
"Time": [
"11:30:01",
"11:30:02",
"11:30:03",
"11:30:03"
],
"elem": [
"H1",
"H1",
"H1",
"BODY"
]
}

我正在尝试使用以下 php 代码:

<?php
$string = file_get_contents("./PHP/JSON/clicks.json");
$json_a = json_decode($string, true);
for($idx = 0; $idx < count($json_a); $idx++){
$obj = (Array)$json_a[$idx];
echo $obj["cX"];
?>

最佳答案

<?php
$string = file_get_contents("./PHP/JSON/clicks.json");
$json_a = json_decode($string);
$cx = $json_a->cX;
$cy = $json_a->cY;
?>

希望对你有帮助

关于php - 将 JSON 文件解析为 PHP 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com