您好,我是 mongodb 新手,我想将 JSONObject 转换为文档,然后将其存储到 mongodb。
这是我编码的内容。我得到了一个 json 格式的服务 api。
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
JSONObject Rat = new JSONObject(EntityUtils.toString(entity));
然后我想将这个 Rat 作为文档保存到 mongodb 中,然后将其插入到 mongodb 或 mysql 中,以便稍后显示它。
我想像
Document doc = new Document(....);
coll.insertOne(doc); /*where coll is
MongoCollection<Document> coll = db.getCollection("rates"); */
但是如何将 JSON 对象转换为文档呢?
MongoDB Java 驱动程序提供了Document.parse(String json)
方法从 JSON 字符串获取 Document 实例。您需要将 JSON 对象解析回字符串,如下所示:
Document doc = Document.parse( Rat.toString() );
这是docs用于在 MongoDB Java 驱动程序中使用 JSON。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)