gpt4 book ai didi

数组的 Javascript/AJAX JSON 编码 - 收到数据后如何使用?

转载 作者:行者123 更新时间:2023-11-28 14:50:46 24 4
gpt4 key购买 nike

我想从服务器上的文件夹中获取所有图像,我通过 ajax 调用来实现

files = glob('assets/img/daily/*'); // get all file names
$imageArr;
foreach ($files as $file) {
$imageArr[] = $file;
}
//$imageJSON = ['img01' => $imgArr[0], 'img02' => $imgArr[1], 'img02' => $imgArr[2]];
//$jsonObj = json_encode($imageArr);
$jsonObj = json_encode($imageArr);

echo $jsonObj;

我可以回显 json 编码,如下所示

["assets\/img\/daily\/163.jpg","assets\/img\/daily\/168.jpg","assets\/img\/daily\/197.jpg","assets\/img\/daily\/223.jpg","assets\/img\/daily\/232.jpg","assets\/img\/daily\/260.jpg","assets\/img\/daily\/297.jpg","assets\/img\/daily\/30.jpg","assets\/img\/daily\/310.jpg","assets\/img\/daily\/333.jpg","assets\/img\/daily\/339.jpg","assets\/img\/daily\/411.jpg","assets\/img\/daily\/421.jpeg","assets\/img\/daily\/427.jpg","assets\/img\/daily\/46.jpg","assets\/img\/daily\/52.jpg","assets\/img\/daily\/86.jpg","assets\/img\/daily\/background.jpg","assets\/img\/daily\/booby.png","assets\/img\/daily\/booty.png"]

更新

这是我的 Javascript/AJAX/HTML

<p id="raw"></p>
<br><br>
<p id="json"></p>
<script>
var raw, json;
raw = document.getElementById("raw");
json = document.getElementById("json");
var http = new XMLHttpRequest();
var url = "ajax.php";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
// alert(this.responseText);
var jsonObj = JSON.parse(this.responseText);
raw.textContent = this.responseText;
json.textContent = jsonObj;
}
};
http.send();
</script>

我如何访问数据?我不能做 jsonObj.name 因为没有输出被命名?我收到的 var jsonObj = JSON.parse(this.responseText) 看起来像这样

assets/img/daily/30.jpg,assets/img/daily/46.jpg,assets/img/daily/background.jpg,assets/img/daily/booby.png,assets/img/daily/booty.png,assets/img/daily/chrome.png,assets/img/daily/default.png,assets/img/daily/defaults.png

最佳答案

But my problem is if i try to get data from this JSON obj like echo $jsonObj[0] i will recieve this as output "["

JSON 是一个字符串。它被设计为通过 HTTP 传输、存储在文件中等。它并非设计为在不首先解析的情况下进行处理。您需要将其转换为数组才能有效使用它。

…除非你不这样做,因为数据已经可用于数组;您将其用作 json_encode 的输入。

只需使用 $imageArr 变量而不是 $jsonObj

关于数组的 Javascript/AJAX JSON 编码 - 收到数据后如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44589943/

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