Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 列为未定义的JSON对象元素(在使用`jQuery.parseJSON()`之前和之后都进行了测试)_Javascript_Ajax_Json - Fatal编程技术网

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

Javascript 列为未定义的JSON对象元素(在使用`jQuery.parseJSON()`之前和之后都进行了测试),javascript,ajax,json,Javascript,Ajax,Json,我试图通过迭代一个JSON对象来填充一个下拉列表,并用row.id附加每个的值=,以及标记与row.name之间的实际值 编辑 我忘了提到所传递的数据已经过Ajax处理,是PHP调用数据库的结果 问题是,在填充列表时,每个条目都列为未定义的。id也一样。我一定是做错了,我只是不知道是什么。这是我拥有的阵列: [{ "id": "1", "0": "1", "parent": "0", "1": "0", "name": "Automotive",

我试图通过迭代一个JSON对象来填充一个下拉列表,并用
row.id
附加每个
值=
,以及
标记与
row.name
之间的实际值

编辑

我忘了提到所传递的数据已经过Ajax处理,是PHP调用数据库的结果

问题是,在填充列表时,每个条目都列为
未定义的
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;
}

仍然不工作…

我认为问题在于你做了太多的解析和检查。如果上面描述的对象是用Javascript正确创建的,则不需要使用解析函数。只有当通过PHP或从DOM中以字符串形式检索对象时,才真正需要这些

您没有指定
类别是什么。我不知道是对象数组还是对象数组的每次迭代,但不管怎样,我认为你的步骤太多了。要做到这一点,你应该只需要这个

…假设
categories
是整个对象数组

for(var obj in arr){
  var opt = document.createElement('option');
  if(obj['id'] && obj['name']){
    opt.text = obj['id'];
    opt.value = obj['name'];
  }
  $('#subcategories').append(opt);
}
for(变量i=0;i

或者您可以使用
$。每个
,都会更好。

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

[{
    "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"
}
在发布的
中有一个换行符,它使JSON无效。删除所有这些换行符将使JSON有效,并且可以再次解析

在这里测试json,看看它是否是有效的json

编辑:

好的,我在JSFIDLE上尝试了代码,这让我意识到json实际上是如何格式化的。数组中有一些对象,这意味着您只需循环数组,然后像平常一样访问对象

看到这个了吗

我试过这个,它对我有效

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

$.each(myObj, function(k, v) {
var opt = document.createElement('option');
$(opt).attr('value', v.id).html(v.name).appendTo($('#mySelect'));
});

我应该指定数组来自php数据库调用。好的,在这种情况下,首先尝试只调用JSON.parse(),一旦在初始对象数组上调用,就不需要在每次迭代中调用它。一旦你这样做了,console.log()就会分析结果,看看会发生什么,这可能有助于判断出哪里出了问题。我已经尝试过这个方法,但不幸的是,它不起作用。我忘了提到收到的数据来自一个PHP数据库调用。如何准确地
$。每个
都会更好?你能告诉我为什么在属性名称的某个部分有很多换行符吗?为什么要复制所有的数据?好眼力,但这不是问题:格式化我帖子的人是偶然做的。我刚刚验证过,它是有效的。请看一下JSFIDLE。我相信我已经找到了解决办法
var myObj = [
{
    "0": "1",
    "1": "0",
    "2": "Automotive",
    "3": null,
    "4": "0",
    "5": "1",
    "id": "1",
    "parent": "0",
    "name": "Automotive",
    "description": null,
    "ordering": "0",
    "published": "1"
},
{
    "0": "2",
    "1": "0",
    "2": "Aerospace",
    "3": null,
    "4": "1",
    "5": "1",
    "id": "2",
    "parent": "0",
    "name": "Aerospace",
    "description": null,
    "ordering": "1",
    "published": "1"
},
{
    "0": "3",
    "1": "0",
    "2": "Agricultural",
    "3": null,
    "4": "2",
    "5": "1",
    "id": "3",
    "parent": "0",
    "name": "Agricultural",
    "description": null,
    "ordering": "2",
    "published": "1"
},
{
    "0": "4",
    "1": "0",
    "2": "Audiovisual",
    "3": null,
    "4": "3",
    "5": "1",
    "id": "4",
    "parent": "0",
    "name": "Audiovisual",
    "description": null,
    "ordering": "3",
    "published": "1"
},
{
    "0": "5",
    "1": "0",
    "2": "Construction",
    "3": null,
    "4": "4",
    "5": "1",
    "id": "5",
    "parent": "0",
    "name": "Construction",
    "description": null,
    "ordering": "4",
    "published": "1"
},
{
    "0": "6",
    "1": "0",
    "2": "Energy",
    "3": null,
    "4": "6",
    "5": "1",
    "id": "6",
    "parent": "0",
    "name": "Energy",
    "description": null,
    "ordering": "6",
    "published": "1"
},
{
    "0": "7",
    "1": "0",
    "2": "Electronics",
    "3": null,
    "4": "7",
    "5": "1",
    "id": "7",
    "parent": "0",
    "name": "Electronics",
    "description": null,
    "ordering": "7",
    "published": "1"
},
{
    "0": "8",
    "1": "0",
    "2": "Electrical",
    "3": null,
    "4": "8",
    "5": "1",
    "id": "8",
    "parent": "0",
    "name": "Electrical",
    "description": null,
    "ordering": "8",
    "published": "1"
},
{
    "0": "9",
    "1": "0",
    "2": "Entertainment",
    "3": null,
    "4": "9",
    "5": "1",
    "id": "9",
    "parent": "0",
    "name": "Entertainment",
    "description": null,
    "ordering": "9",
    "published": "1"
},
{
    "0": "10",
    "1": "0",
    "2": "Manufacturing",
    "3": null,
    "4": "9",
    "5": "1",
    "id": "10",
    "parent": "0",
    "name": "Manufacturing",
    "description": null,
    "ordering": "9",
    "published": "1"
},
{
    "0": "11",
    "1": "0",
    "2": "Medical",
    "3": null,
    "4": "11",
    "5": "1",
    "id": "11",
    "parent": "0",
    "name": "Medical",
    "description": null,
    "ordering": "11",
    "published": "1"
},
{
    "0": "12",
    "1": "0",
    "2": "Marine",
    "3": null,
    "4": "12",
    "5": "1",
    "id": "12",
    "parent": "0",
    "name": "Marine",
    "description": null,
    "ordering": "12",
    "published": "1"
},
{
    "0": "13",
    "1": "0",
    "2": "Home Applicances",
    "3": null,
    "4": "13",
    "5": "1",
    "id": "13",
    "parent": "0",
    "name": "Home Applicances",
    "description": null,
    "ordering": "13",
    "published": "1"
},
{
    "0": "14",
    "1": "0",
    "2": "Software",
    "3": null,
    "4": "14",
    "5": "1",
    "id": "14",
    "parent": "0",
    "name": "Software",
    "description": null,
    "ordering": "14",
    "published": "1"
},
{
    "0": "15",
    "1": "0",
    "2": "Theoretical work",
    "3": null,
    "4": "15",
    "5": "1",
    "id": "15",
    "parent": "0",
    "name": "Theoretical work",
    "description": null,
    "ordering": "15",
    "published": "1"
},
{
    "0": "16",
    "1": "0",
    "2": "Railroad",
    "3": null,
    "4": "16",
    "5": "1",
    "id": "16",
    "parent": "0",
    "name": "Railroad",
    "description": null,
    "ordering": "16",
    "published": "1"
},
{
    "0": "77",
    "1": "0",
    "2": "Chemistry",
    "3": null,
    "4": "5",
    "5": "1",
    "id": "77",
    "parent": "0",
    "name": "Chemistry",
    "description": null,
    "ordering": "5",
    "published": "1"
},
{
    "0": "158",
    "1": "0",
    "2": "Empty Category",
    "3": null,
    "4": "17",
    "5": "1",
    "id": "158",
    "parent": "0",
    "name": "Empty Category",
    "description": null,
    "ordering": "17",
    "published": "1"
}];

$.each(myObj, function(k, v) {
var opt = document.createElement('option');
$(opt).attr('value', v.id).html(v.name).appendTo($('#mySelect'));
});