我需要从以下 json 中获取所有名称字段的列表。我试图通过映射一个 DBObject 来做到这一点。
JSON:
{
"firstOne": [
{
"file": "FileA",
"Data": "One",
"version": "0.4"
}
],
"secondOne": [
{
"elementName": "version",
"complexElement": "true",
"elementDataType": ""
}
],
"ThirdOne": [
{
"elementName": "version",
"explicitElements": [
{
"name": "mytag",
"type": "String",
"value": "myrequest"
},
{
"name": "booleantest",
"type": "Boolean",
"value": "true"
}
]
}
] }
代码:
DBCollection 集合 = mongoTemplate.getCollection("testTag");
Map<DBObject, DBObject> map = new HashMap<DBObject, DBObject>(); DBObject obj =null; BasicDBObject query = new BasicDBObject(); query.put("ThirdOne.explicitElements.name", "myTag"); BasicDBObject fields = new BasicDBObject(); fields.put("ThirdOne.explicitElements.name", 1); fields.put("ThirdOne.explicitElements.value", 1); DBCursor cursor2 = collection.find(query, fields); while
(cursor2.hasNext()) { obj = cursor2.next(); 地图 = obj.toMap();
for (Map.Entry<DBObject, DBObject> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } }
结果:
键:_id 值:523f910681a9535f8af5aa91
键:ThirdOne 值:[{ "explicitElements" : [{"name":"mytag", "value":"myrequest"},{"name":"booleantest", "value":"true"}]}]
我只想得到
键:“名称” 值:“mytag”
键:“值”值:“我的请求”等...
有关如何使用 DBObjects 执行此操作的任何建议?