无法使用Jquery循环复杂的JSON对象

时间:2014-03-25 11:52:25

标签: jquery json

我试图循环遍历以下JSON对象但没有成功:

[
    {
        "Count": 0,
        "GroupId": 1,
        "Icon": null,
        "Service": [
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 1,
                "Type": "Cat"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 2,
                "Type": "Dog"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 3,
                "Type": "Whale"
            }
        ],
        "Type": "Animal carcass removal"
    }
]    

我需要访问'Service'对象并将Type附加到下拉列表中?我怀疑我可能需要一个循环中的每个循环似乎不能正确吗?

任何帮助都将不胜感激。

由于

5 个答案:

答案 0 :(得分:1)

你应该可以用

来做
$.each(data[0].Service, function () {
    $('select').append('<option val="'+this.Type+'">'+this.Type+'</option>')
});

其中data表示您问题中的对象。

参见 live fiddle

答案 1 :(得分:0)

试试这个

            var data = [
    {
        "Count": 0,
        "GroupId": 1,
        "Icon": null,
        "Service": [
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 1,
                "Type": "Cat"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 2,
                "Type": "Dog"
            },
            {
                "Count": 0,
                "Icon": null,
                "ServiceId": 3,
                "Type": "Whale"
            }
        ],
        "Type": "Animal carcass removal"
    }
]

        for (i = 0; i < data[0].Service.length; i++) {
            $("#slct").append($("<option/>", { html: data[0].Service[i].Type, value: data[0].Service[i].ServiceId }));
        }

HTML

<select id="slct">
</select>

答案 2 :(得分:0)

$.each(a[0].Service,function (i,n){

  $("#ddl").append($("<option></option>").val(n.ServiceId).text(n.Type));


})

http://jsbin.com/gigusora/2/edit

答案 3 :(得分:0)

试试这个

var jso=[
{
    "Count": 0,
    "GroupId": 1,
    "Icon": null,
    "Service": [
        {
            "Count": 0,
            "Icon": null,
            "ServiceId": 1,
            "Type": "Cat"
        },
        {
            "Count": 0,
            "Icon": null,
            "ServiceId": 2,
            "Type": "Dog"
        },
        {
            "Count": 0,
            "Icon": null,
            "ServiceId": 3,
            "Type": "Whale"
        }
    ],
    "Type": "Animal carcass removal"
}

] 
var ser=jso[0].Service;
var output='';
for(var i in ser)
{
   output+='<option value="'+ser[i].Type+'">'+ser[i].Type+'</option>';
}
$('select').append(output)

DEMO

答案 4 :(得分:0)

试一试:

var data = [{
"Count": 0,
    "GroupId": 1,
    "Icon": null,
    "Service": [{
    "Count": 0,
        "Icon": null,
        "ServiceId": 1,
        "Type": "Cat"
}, {
    "Count": 0,
        "Icon": null,
        "ServiceId": 2,
        "Type": "Dog"
}, {
    "Count": 0,
        "Icon": null,
        "ServiceId": 3,
        "Type": "Whale"
}],
    "Type": "Animal carcass removal"
}];

for(var d in data){
   var service = data[d].Service;
   for(var i = 0;i < service.length;i += 1){
       console.log(service[i].type);
   }
}