通过Jquery JSON解析
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到 jquery 唯一可以帮助你的是
.each()
函数。这是一个示例,我浏览了每个客户的每个文件名。更新了代码以包含保存 CustomerPortals 的门户。如果我错误地解释了您的评论,请告诉我。
再次更新代码以使用新的示例代码。示例代码导致错误,因此我在“Portals”及其值周围添加了 { }。如果这不是代码的预期格式,请告诉我。
编辑
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.
EDIT
我对此表示怀疑。 JSON 对象层次结构作为 DOM 节点不可见。
您可以使用地图函数,这很容易 编写,并且可以在函数式 JavaScript。使用它很简单:
然后对文件名数组执行任何您想要的操作。
编辑:为了确认这一点,我在 JavaScript 中运行了以下代码测试台。
它显示了一条警报,其中包含以下消息:
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:
Then do whatever you want with the array of filenames.
EDIT: To confirm this, I ran the following code in a JavaScript test bench.
It displayed an alert with the following message: