JSONObject到文档 [英] JSONObject to Document

查看:106
本文介绍了JSONObject到文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb的新手,我想将JSONObject转换为Document,然后将其存储到mongodb. 这是我编写的代码.我在json中获得了一个服务api.

Hi there i am new with mongodb and i want to convert JSONObject to Document and then store it to mongodb. Here is what i have coded.I get a service api in json.

CloseableHttpResponse response = httpClient.execute(get);
        HttpEntity entity = response.getEntity();          
        JSONObject Rat = new JSONObject(EntityUtils.toString(entity));

然后我要将该Rat保存为mongodb作为文档,然后将其插入mongodb或mysql中,以便以后显示. 我以为

then i want to save this Rat to a mongodb as a document and then insert it to mongodb or to mysql so that i can display it later. I thought something like

Document  doc = new Document(....);
coll.insertOne(doc); /*where coll is 
MongoCollection<Document> coll = db.getCollection("rates"); */

但是如何进行从JSONObject到Document的转换?

but how to do the convertion from JSONObject to Document?

推荐答案

只为您提供帮助.您也可以这样做:

Just to help you. You can also do:

JSONObject rat = exRat.getJSONObject("fieldfromJson");String newrat = rat.toString();

然后,您必须解析所需的字段,并且您有一个字符串.

Then you have to parse the field you want, and you have a string.

这篇关于JSONObject到文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆