通过Jquery JSON解析

发布于 2024-11-02 16:10:23 字数 2458 浏览 0 评论 0原文

Test.Customers = [{
    "CustomerPortals":[{
        "filename":"chrysanthemum.jpg",
        "ext":"jpg",
        },{
        "name":"Lighthouse.jpg",
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        },{
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        }
        }]

我把这个 json 存储在我的 DOM 中。有时 CustomerPortals 数组中有 0 个项目,有时有多个项目。但我想解析它们并显示文件名。 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"}
                                         ]
                            }
                    }
                ]

我想获取所有人的门户名称 客户。以上不是 显示子数组谢谢

Test.Customers = [{
    "CustomerPortals":[{
        "filename":"chrysanthemum.jpg",
        "ext":"jpg",
        },{
        "name":"Lighthouse.jpg",
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        },{
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        }
        }]

I have this json stored in my DOM. Sometimes there will be 0 items and sometimes multiple items in CustomerPortals array. But I want to parse through them and show the filename. Does jquery make it any simpler to do that?

EDIT

EDIT

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"}
                                         ]
                            }
                    }
                ]

I want to get the portalname for all
the customers. the above was not
showing sub arrays thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2024-11-09 16:10:23

我能想到 jquery 唯一可以帮助你的是 .each() 函数。这是一个示例,我浏览了每个客户的每个文件名。

更新了代码以包含保存 CustomerPortals 的门户。如果我错误地解释了您的评论,请告诉我。

再次更新代码以使用新的示例代码。示例代码导致错误,因此我在“Portals”及其值周围添加了 { }。如果这不是代码的预期格式,请告诉我。

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"}
                                             ]
                                }
                            }
                    }
                ]




$(document).ready(function() {

    //Go through each customer
    $.each(Test.Customers, function(customerIndex, customerValue) {

        //Go through each CustomerPortal and display filename
        $.each(customerValue.CustomerItems.Portals.CustomPortals, function(custPortIndex, custPortValue) {

            alert('File ' + custPortValue.portalname + ' in customer ' + customerIndex);

        });

    });

});

编辑

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"}
                                         ]
                            }
                    }
                ]

我想获取所有人的门户名称
客户。以上不是
显示子数组谢谢

The only thing I can think of that jquery can help you with is the .each() function. Here is an example where I go through each filename in each customer.

Updated code to include Portals which holds the CustomerPortals. Let me know if I interpreted your comment incorrectly.

Updated code again to use the new example code. The example code was causing errors so I added { } around "Portals" and its value. If that wasn't the intended format for the code let me know.

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"}
                                             ]
                                }
                            }
                    }
                ]




$(document).ready(function() {

    //Go through each customer
    $.each(Test.Customers, function(customerIndex, customerValue) {

        //Go through each CustomerPortal and display filename
        $.each(customerValue.CustomerItems.Portals.CustomPortals, function(custPortIndex, custPortValue) {

            alert('File ' + custPortValue.portalname + ' in customer ' + customerIndex);

        });

    });

});

EDIT

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"}
                                         ]
                            }
                    }
                ]

I want to get the portalname for all
the customers. the above was not
showing sub arrays thanks

葬花如无物 2024-11-09 16:10:23

我对此表示怀疑。 JSON 对象层次结构作为 DOM 节点不可见。

您可以使用地图函数,这很容易 编写,并且可以在函数式 JavaScript。使用它很简单:

filenames = Test.Customers[0].CustomerPortals.map(function(cp) { return cp.filename });

然后对文件名数组执行任何您想要的操作。

编辑:为了确认这一点,我在 JavaScript 中运行了以下代码测试台

Test = {};

Test.Customers = [{
    "CustomerPortals":[{
        "filename":"chrysanthemum.jpg",
        "ext":"jpg",
        },{
        "name":"Lighthouse.jpg",
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        },{
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        }]
        }];

Array.prototype.map = function(fn) {
              var r = [];
              var l = this.length;
              for(i=0;i<l;i++)
              {
                  r.push(fn(this[i]));
              }
              return r; 
            };

alert(Test.Customers[0].CustomerPortals.map(function(cp) { return cp.filename }));

它显示了一条警报,其中包含以下消息:

菊花.jpg,灯塔.jpg,灯塔.jpg

I doubt it. JSON object hierarchies aren't visible as DOM nodes.

You could use a map function, which is very easy to write, and readily available in libraries like Functional JavaScript. Using it is simple:

filenames = Test.Customers[0].CustomerPortals.map(function(cp) { return cp.filename });

Then do whatever you want with the array of filenames.

EDIT: To confirm this, I ran the following code in a JavaScript test bench.

Test = {};

Test.Customers = [{
    "CustomerPortals":[{
        "filename":"chrysanthemum.jpg",
        "ext":"jpg",
        },{
        "name":"Lighthouse.jpg",
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        },{
        "filename":"lighthouse.jpg",
        "ext":"jpg",
        }]
        }];

Array.prototype.map = function(fn) {
              var r = [];
              var l = this.length;
              for(i=0;i<l;i++)
              {
                  r.push(fn(this[i]));
              }
              return r; 
            };

alert(Test.Customers[0].CustomerPortals.map(function(cp) { return cp.filename }));

It displayed an alert with the following message:

chrysanthemum.jpg,lighthouse.jpg,lighthouse.jpg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文