通过对象解析
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);
});
});
我可以得到一个工作的迭代器,你可以在这里看到它的作用
相关文章:
- 引用对象中的通用值
- jQuery匹配JSON对象的部分文本
- 节点导出返回一个空对象
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 全局变量和全局对象的属性之间有什么区别吗
- 比较从函数和生成的日期对象
- Javascript,访问一个主要对象模块模式中的每个对象
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 调整窗口大小时,可拖动的对象会出现在容器外部
- 如何使用javascript从主svg对象动态创建svg视图框
- 如何使用json将对象列表从java转换为javascript
- 序列化数据属性中对象的最可靠方法
- 如何访问声音管理器2创建的声音对象
- FabricJs-限制主对象内添加对象的移动区域
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 值对象在某个变量发生更改后发生更改
- 如何为json对象中的段发送array[]
- Chrome开发工具(如何知道我在调用哪个javascript对象)