我有一个 String
和一个包含列表的 JSON。它是这样的:
{
"counts":[
{"foo827138": 123124, "bar2918":312312, "something_else321-313":2321321},
{"foo1231412": 4321, "bar1231515":123, "something_else3012931":7282},
{"foo1210820": 1234, "bar10293810":3112, "something_else12094":1321}
]
}
用 BasicDBObject.parse()
解析后:
DBObject doc = BasicDBObject.parse(jsonString);
如何获取 counts
列表中的键和值?
PS.: doc.get("counts")
返回一个 BasicDBList
对象。我只能通过以下方式迭代:
for (Object a : doc) {
// Something
}
Object
不提供 get()
方法或类似方法。所以我不知道如何获取我的任何键名和相应的值。
最佳答案
Document document = Document.parse(jsonString);
List<Document> countsArray = document.get("counts",List.class);
for (Document doc : countsArray){
doc.keySet(); //keys
doc.values(); //values
for (Map.Entry entry : doc.entrySet()){
// iterate over each pair
}
}
关于java - 使用 BasicDBObject 从 JSON 字符串列表中获取键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949164/