嗨,我是mongodb的新手,我想将JSONObject转换为Document,然后将其存储到mongodb.这是我编码的内容.我在json中获得了一个服务api.
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
JSONObject Rat = new JSONObject(EntityUtils.toString(entity));
Run Code Online (Sandbox Code Playgroud)
然后我想将这个鼠保存为mongodb文件,然后将其插入mongodb或mysql,以便我以后可以显示它.我觉得有点像
Document doc = new Document(....);
coll.insertOne(doc); /*where coll is
MongoCollection<Document> coll = db.getCollection("rates"); */
Run Code Online (Sandbox Code Playgroud)
但是如何从JSONObject转换为Document呢?
met*_*ame 17
MongoDB Java驱动程序提供了Document.parse(String json)从JSON字符串获取Document实例的方法.您需要将JSON对象解析回String,如下所示:
Document doc = Document.parse( Rat.toString() );
Run Code Online (Sandbox Code Playgroud)
这是在MongoDB Java驱动程序中使用JSON 的文档.
小智 1
只是为了帮助你。您还可以这样做:
JSONObject rat = exRat.getJSONObject("fieldfromJson");String newrat = rat.toString();
Run Code Online (Sandbox Code Playgroud)
然后你必须解析你想要的字段,你就有了一个字符串。
| 归档时间: |
|
| 查看次数: |
12891 次 |
| 最近记录: |