通过对象解析

Parsing through object

本文关键字:对象      更新时间:2023-09-26

我有这个对象,我想获得portalname。怎么做呢,用JS或者jQuery

var Test = {};
Test.Customers = [{"CustomerItems":
                            "Portals":
                            {"id":"1","customer":"1950","resident":"yes",
                            "CustomPortals":
                                        [
                                        {"id":"11","test":"4251","portalname":"tye.jpg"},
                                         {"id":"12","test":"4251","portalname":"Lsdf.jpg"},
                                         {"id":"13","test":"4251","portalname":"nick.jpg"}
                                         ]
                            }
                    },
                    {"CustomerItems":
                            "Portals":
                            {"id":"2","customer":"1952","resident":"yes",
                            "CustomPortals":
                                        [
                                        {"id":"14","test":"4252","portalname":"Chrysanthemum2.jpg"},
                                         {"id":"15","test":"4255","portalname":"navagin.jpg"},
                                         {"id":"16","test":"4257","portalname":"jasoria.jpg"}
                                         ]
                            }
                    },
                    {"CustomerItems":
                            "Portals":
                            {"id":"3","customer":"1950","resident":"yes",
                            "CustomPortals":
                                        [
                                        {"id":"17","test":"4231","portalname":"Zsryanmum1.jpg"},
                                         {"id":"18","test":"4651","portalname":"Ltd1.jpg"},
                                         {"id":"19","test":"4281","portalname":"ser1.jpg"}
                                         ]
                            }
                    }
                ]

尝试

 $.each(Test.Customers, function(index, value) {
                $.each(value.CustomPortals, function(innerIndex, innerValue) {
                    alert('File ' + innerValue + ' in customer ' + innerIndex);
                });
            });

但是行不通

你的内部".each()"循环需要遍历"CustomerItems "。进入"CustomPortals":

            $.each(value.CustomerItems.Portals.CustomPortals, function(innerIndex, innerValue) {
                alert('File ' + innerValue + ' in customer ' + innerIndex);
            });

添加一些存在性测试可能是个好主意,但你比我更了解你的数据。

编辑本;@justkt有一个非常好的观点——正如这里所发布的那样,JSON首先是无效的。因此,如果内容可以被解析,那么我上面写的内容将是正确的:-)

Try

alert('File ' + innerValue.portalname + ' in customer ' + innervalue.id);
不是

alert('File ' + innerValue + ' in customer ' + innerIndex);

当我通过JSONLint运行你的对象时(虽然我知道JSON比JS对象更严格,但它是一个简单的验证器),它抱怨这个语法:

"CustomerItems" : "Portals" : {}

通过删除"Portals"并改为设置:

"CustomerItems" : {}

和使用下面的JS:

$.each(Test.Customers, function(index, value) {
    $.each(value.CustomerItems.CustomPortals, function(innerIndex, innerValue) {
        alert('File ' + innerValue.portalname + ' in customer ' + innerValue.id);
    });
});

我可以得到一个工作的迭代器,你可以在这里看到它的作用