本文介绍了使用Java解析聚合输出mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
"serverUsed" : "localhost/127.0.0.1:27017",
"result" : [{
"_id" : {
"$oid" : "529f131430044109e30fc6f9"
},
"html" : {
"table" : {
"tbody" : {
"Barge" : {
"Name" : "ANTVERPIA 56",
"Bargeno" : 6003696,
"Harbour" : "HH",
"Reportedpresent" : " ",
"Starting" : "06-12-2013 spil 2"
}
}
}
}
}]
}
我将其作为结果,如何获得Name的字符串值.在这种情况下,ANTVERPIA 56.我已经尝试使用以下代码,但无法正常工作,请帮忙.
I have this as result, how can i get string value of Name. In this case ANTVERPIA 56. I have tried with this following code but it does not working, please help.
for (DBObject result1: output.results()){
String name1 = (String)result1.get("html.table.tbody.Barge.Name");
System.out.println(name1);
}
推荐答案
您不能使用"来访问嵌套对象.在Java驱动程序中.您必须为每个嵌套的json对象获取DBObject.以下代码可以解决问题.
You cannot access nested objects by using "." in Java driver. You have to get DBObject for each nested json object. Following code should solve problem.
for (DBObject result : output.results()) {
DBObject htmlObj = (DBObject) result.get("html");
DBObject tableObj = (DBObject) htmlObj.get("table");
DBObject tbodyObj = (DBObject) tableObj.get("tbody");
DBObject bargeObj = (DBObject) tbodyObj.get("Barge");
String name = (String) bargeObj.get("Name");
}
这篇关于使用Java解析聚合输出mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!