gpt4 book ai didi

javascript - JSON 对象元素列为未定义(在使用 `jQuery.parseJSON()` 之前和之后进行测试)

转载 作者:行者123 更新时间:2023-11-29 15:02:07 27 4
gpt4 key购买 nike

我正在尝试通过遍历 JSON 对象并附加 value= 来填充下拉列表每个 <option>row.id<option> 之间的实际值带有 row.name 的标签.

编辑

我忘了说传递的数据是 Ajaxed,是 PHP 调用数据库的结果。

问题是,在填充列表时,每个条目都列为 undefined . id 同上以及。我一定做错了,我只是不确定是什么。这是我的数组:

    [{
"id": "1",
"0": "1",
"parent": "0",
"1": "0",
"name": "Automotive",
"2": "Automotive",
"description": null,
"3": null,
"ordering": "0",
"4": "0",
"pub
lished": "1",
"5": "1"
}, {
"id": "2",
"0": "2",
"parent": "0",
"1": "0",
"name": "Aerospace",
"2": "Aerospace",
"description": null,
"3": null,
"ordering
": "1",
"4": "1",
"published": "1",
"5": "1"
}, {
"id": "3",
"0": "3",
"parent": "0",
"1": "0",
"name": "Agricultural",
"2": "Agricultural",
"description": null,
"3": null,
"ordering": "2",
"4": "2",
"published": "1",
"5": "1"
}, {
"id": "4",
"0": "4",
"parent": "0",
"1": "0",
"name": "Audiovisual",
"2": "Audiovisual",
"description": null,
"3": null,
"ordering": "3",
"4": "3",
"published": "1",
"5": "1"
}, {
"id": "5",
"0": "5",
"parent": "0",
"1": "0",
"name": "
Construction",
"2": "Construction",
"description": null,
"3": null,
"ordering": "4",
"4": "4",
"published": "1",
"5": "1"
}, {
"id": "6",
"0": "6",
"parent": "0",
"1": "0",
"name": "Energy",
"2": "Energy",
"description": null,
"3": null,
"ordering": "6",
"4": "6",
"published": "1",
"5": "1"
}, {
"id": "7",
"
0": "7",
"parent": "0",
"1": "0",
"name": "Electronics",
"2": "Electronics",
"description": null,
"3": null,
"ordering": "7",
"4": "7",
"published": "1
",
"5": "1"
}, {
"id": "8",
"0": "8",
"parent": "0",
"1": "0",
"name": "Electrical",
"2": "Electrical",
"description": null,
"3": null,
"ordering": "8",
"4
": "8",
"published": "1",
"5": "1"
}, {
"id": "9",
"0": "9",
"parent": "0",
"1": "0",
"name": "Entertainment",
"2": "Entertainment",
"description": null,
"3": null,
"ordering": "9",
"4": "9",
"published": "1",
"5": "1"
}, {
"id": "10",
"0": "10",
"parent": "0",
"1": "0",
"name": "Manufacturing",
"2": "Manufa
cturing",
"description": null,
"3": null,
"ordering": "9",
"4": "9",
"published": "1",
"5": "1"
}, {
"id": "11",
"0": "11",
"parent": "0",
"1": "0",
"name": "Medical",
"2": "Medical",
"description": null,
"3": null,
"ordering": "11",
"4": "11",
"published": "1",
"5": "1"
}, {
"id": "12",
"0": "12",
"parent": "0",
"1": "0",
"name": "Marine",
"2": "Marine",
"description": null,
"3": null,
"ordering": "12",
"4": "12",
"published": "1",
"5": "1"
}, {
"id": "13",
"0": "13",
"parent": "0",
"1": "0",
"name": "Home Applicances",
"2": "Home Applicances",
"description": null,
"3": null,
"ordering": "13",
"4": "13",
"published": "1",
"5": "1"
}, {
"id": "14",
"0": "14",
"parent": "0",
"1": "0",
"name": "Software",
"2": "Software",
"description": null,
"3": null,
"ordering": "14",
"4": "14",
"published": "1",
"5": "1"
}, {
"id": "15",
"0": "15",
"parent": "0",
"1": "0",
"name": "Theoretical work",
"2": "Theoretical work",
"description": null,
"3": null,
"ordering": "15",
"4": "15",
"published": "1",
"5": "1"
}, {
"id": "16",
"0": "16",
"parent": "0",
"1": "0",
"name": "Railroad",
"2": "Railroad",
"description": null,
"3": null,
"ordering": "16",
"4": "16",
"published": "1",
"5": "1"
}, {
"id": "77",
"0": "77",
"parent": "0",
"1": "0",
"name": "Chemistry",
"2": "Chemistry",
"description": null,
"3": null,
"ordering": "5",
"4": "5",
"published": "1",
"5": "1"
}, {
"id": "158",
"0": "158",
"parent": "0",
"1": "0",
"name": "Empty Category",
"2": "Empty Category",
"description": null,
"3": null,
"ordering": "17",
"4": "17",
"published": "1",
"5": "1"
}]

这是我的代码:

function printSubCategories(categories) {

for (var row in categories) {

rowParsed = jQuery.parseJSON(row);

var opt = document.createElement('option');

if (categories.hasOwnProperty(row)) {
opt.text = rowParsed.name;
opt.value = rowParsed.id;
}

jQuery('#subcategories').append(opt);
}

}

我究竟做错了什么?

更新

尝试了以下实现:

if (categories.hasOwnProperty(row)) {

idParsed = jQuery.parseJSON(row.id);
nameParsed = jQuery.parseJSON(row.name)

opt.text = idParsed;
opt.value = nameParsed;
}

还是不行...

最佳答案

数据的意外区域有一些换行符。比如,在

[{
"id": "1",
"0": "1",
"parent": "0",
"1": "0",
"name": "Automotive",
"2": "Automotive",
"description": null,
"3": null,
"ordering": "0",
"4": "0",
"pub
lished": "1",
"5": "1"
}

published 中有一个换行符,它使 JSON 无效。删除所有这些换行符应该会使 JSON 有效并且可以再次解析。

在这里测试你的 json,看看它是否是一个有效的 json http://jsonlint.com/

编辑:

好的,我已经尝试了 jsfiddle 上的代码,这让我意识到 json 的实际格式。数组中有一些对象,这意味着您只需循环数组,然后像往常一样访问对象。

看这个jsfiddle http://jsfiddle.net/vErj4/

关于javascript - JSON 对象元素列为未定义(在使用 `jQuery.parseJSON()` 之前和之后进行测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8275245/

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