gpt4 book ai didi

javascript - 通过 $.each 循环中的键读取 JSON 数据值

转载 作者:行者123 更新时间:2023-12-01 01:43:37 29 4
gpt4 key购买 nike

我已经从 Controller 返回了 JSON 数据(通过 ajax),并且想要访问该数据。这是一个对象列表(数组):键-值,所以我想使用 .each() 来访问所有数据。像这样的数组:

[{"filePath":"Desktop.zip","fileStatus":"Uploaded"},{"filePath":"Desktop\\dates.xml","fileStatus":"Uploaded"}]

代码是:

$.ajax({
url: '@Url.Action("GetFilesNames", "Home")',
type: 'POST',
success: function (data) {

$.each(data, function (value) {
console.log(value['filePath'], value['fileStatus']);

});
}
});

First picture, data isn't loaded

但是 data.each 值未定义。

所以我尝试console.log所有数据,对其进行json.stringify,解析它(但不知何故出现错误),并解析stringyfied版本,但毫无意义。即使我使用此字符串化版本(路径)或解析的字符串化版本(listOfFiles)与 .each - 相同的结果:未定义。

enter image description here

最佳答案

参见the documentation for jQuery.each :

callback
Type: Function( Integer indexInArray, Object value )

现在查看您的代码:

$.each(data, function (value) {

您正在尝试从 first 参数读取属性,该参数是一个 Integer (array() 中的索引,而不是值。您需要从第二个参数读取属性。

 $.each(data, function (index, value) {

关于javascript - 通过 $.each 循环中的键读取 JSON 数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52151181/

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