将json嵌套对象从mongodb映射到java
我正在使用virustotal API在我的mongodb上获取json对象 以下是存储在mongodb对象中的json对象的外观:将json嵌套对象从mongodb映射到java,java,json,mongodb,pojo,Java,Json,Mongodb,Pojo,我正在使用virustotal API在我的mongodb上获取json对象 以下是存储在mongodb对象中的json对象的外观: { "_id" : ObjectId("597cd2f871eac714388b2f7f"), "results" : { "scans" : { "Bkav" : { "detected" : true, "version" : "1.3.0.8042", "resu
{
"_id" : ObjectId("597cd2f871eac714388b2f7f"),
"results" : {
"scans" : {
"Bkav" : {
"detected" : true,
"version" : "1.3.0.8042",
"result" : "W32.HfsAutoB.971A",
"update" : "20160706"
},
"TotalDefense" : {
"detected" : false,
"version" : "37.1.62.1",
"result" : null,
"update" : "20160706"
},
"MicroWorld-eScan" : {
"detected" : true,
"version" : "12.0.250.0",
"result" : "Packer.Expressor.B",
"update" : "20160706"
},
"nProtect" : {
"detected" : true,
"version" : "2016-07-06.01",
"result" : "Packer.Expressor.B",
"update" : "20160706"
},
"ALYac" : {
"detected" : false,
"version" : "1.0.1.9",
"result" : null,
"update" : "20160706"
},
"TrendMicro" : {
"detected" : true,
"version" : "9.740.0.1012",
"result" : "TROJ_GEN.R047C0CAP16",
"update" : "20160706"
},
"McAfee-GW-Edition" : {
"detected" : true,
"version" : "v2015",
"result" : "BehavesLike.Win32.Flyagent.cc",
"update" : "20160706"
},
"Sophos" : {
"detected" : true,
"version" : "4.98.0",
"result" : "W32/Pidgeon-A",
"update" : "20160706"
},
"Cyren" : {
"detected" : true,
"version" : "5.4.16.7",
"result" : "W32/SysVenFak.A.gen!Eldorado",
"update" : "20160706"
},
"Microsoft" : {
"detected" : true,
"version" : "1.1.12902.0",
"result" : "Backdoor:Win32/Delf.SJ",
"update" : "20160706"
},
"AegisLab" : {
"detected" : true,
"version" : "4.2",
"result" : "Backdoor.W32.BlackHole.acx!c",
"update" : "20160706"
},
"Qihoo-360" : {
"detected" : false,
"version" : "1.0.0.1120",
"result" : null,
"update" : "20160706"
}
},
"scan_id" : "2ad6e0aad0b40f152f234787daa4afb87538f3278f5c8f815d53ef46d5eea4ac-1467833095",
"sha1" : "c5dcd5526ac5330ad1e9fad51488718329fdb697",
"resource" : "0a60424e0967b6cfc172dac82e10a2fe",
"response_code" : 1,
"scan_date" : "2016-07-06 19:24:55",
"permalink" : "https://www.virustotal.com/file/2ad6e0aad0b40f152f234787daa4afb87538f3278f5c8f815d53ef46d5eea4ac/analysis/1467833095/",
"verbose_msg" : "Scan finished, information embedded",
"total" : 54,
"positives" : 41,
"sha256" : "2ad6e0aad0b40f152f234787daa4afb87538f3278f5c8f815d53ef46d5eea4ac",
"md5" : "0a60424e0967b6cfc172dac82e10a2fe"
},
"response_code" : 200
}
正如您所见,json对象太复杂,无法从中获取给定值,
这就是我迄今为止所尝试的:
MongoClient mongo = new MongoClient("localhost", 27017);
MongoDatabase database1 = mongo.getDatabase(db);
MongoCollection<Document> collection1 = database1.getCollection(col);
try (MongoCursor<Document> cursor = collection1.find().iterator()){
while (cursor.hasNext()){
Document doc = cursor.next();
List list = new ArrayList(doc.values());
System.out.println(list.get(1));
}
}
MongoClient mongo=新的MongoClient(“localhost”,27017);
MongoDatabase数据库1=mongo.getDatabase(db);
MongoCollection collection1=database1.getCollection(col);
try(MongoCursor=collection1.find().iterator()){
while(cursor.hasNext()){
Document doc=cursor.next();
列表=新的ArrayList(doc.values());
System.out.println(list.get(1));
}
}
我在想也许有一种方法可以将所有这些json映射到一个java类,主要问题在于“扫描”
,因为有许多不同的扫描程序,并且没有优化为每个扫描程序创建一个java类模型,
我的问题是如何将json对象直接存储到java对象中,以便对返回的结果进行操作。我将您的主模型称为Scan。您可以创建具有以下属性的POJO(我们称之为扫描仪):
扫描仪名称、检测到的、版本、结果、更新 Scanner.java
private String scannerName;
private String detected;
private String version;
private String result;
private String update;
Scan.java
private String scan_id;
private List<Scanner> = new ArrayList<Scanner>();
private String sha1;
private String resource;
......
........
私有字符串扫描\u id;
private List=new ArrayList();
私有字符串sha1;
私有字符串资源;
......
........
因此,您的扫描模型现在有一个扫描仪列表
使用morphia,它类似于:
@Embedded
private List<Scanner> scanner;
@Embedded
私人名单扫描器;
如果您没有在java驱动程序周围使用任何包装器,请尝试
private List<BasicDBObject>
私有列表
您需要创建两个java类来解析来自Mongo的Json,假设第一个类名为“CollectionReceived”
第二类名称“结果”
在CollectionReceived类中,您需要声明成员
public String _id;
public String response_code;
public Result result;
Map<String,Map<String,Map<String,Map<String,Object>>>> results=new HashMap<String,Map<String,Map<String,Map<String,Object>>>>();,
public String scan_id;
public String sha1;
public String resource;
public String response_code;
public String scan_date;
public String permalink;
public String verbose_msg;
public String total;
public String positives;
public String sha256;
public String md5;
在结果类中,您需要声明成员
public String _id;
public String response_code;
public Result result;
Map<String,Map<String,Map<String,Map<String,Object>>>> results=new HashMap<String,Map<String,Map<String,Map<String,Object>>>>();,
public String scan_id;
public String sha1;
public String resource;
public String response_code;
public String scan_date;
public String permalink;
public String verbose_msg;
public String total;
public String positives;
public String sha256;
public String md5;
Map results=new HashMap();,
公共字符串扫描id;
公共字符串sha1;
公共字符串资源;
公共字符串响应_码;
公共字符串扫描日期;
公共字符串permalink;
公共字符串详细信息;
公共字符串总数;
公共字符串正值;
公共字符串sha256;
公共字符串md5;
别忘了在你的控制器上使用
@RestController
,你将很容易解析Json并获得你需要的所有值。我对你的建议有点困惑,正如我所说,有很多扫描仪,如何将它们存储在一个列表中,你能提供一个例子吗,我似乎听不懂你的回答,谢谢你我不太明白你想说什么,为什么我要添加@RestController
假设我想获得BKav
的版本
,我怎样才能得到它?Bkav是HashMap,你可以根据它的键得到它的值,在你的例子中,键是version@RestController将把Json解析为java对象。这是Spring boot中使用的注释如果我没有弄错的话,我不会使用它