This question already has an answer here:
这个问题在这里已有答案:
- JSONObject to Document 2 answers
JSONObject到Document 2的答案
I'm using the below sample json :
我正在使用以下示例json:
JSONObject json=new JSONObject();
json.put("time_range", "22-23");
json.put("flow_id", "786");
And trying to convert to Document as follows :
并尝试转换为Document如下:
Document doc = (Document) JSON.parse(jsonlist.toString()); // conversion from json to Document
col.insertOne(doc); // inserting into Mongo collection
I'm facing the below error:
我面临以下错误:
java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to org.bson.Document
Can anyone please help me out regarding this issue ...
任何人都可以帮我解决这个问题......
2 个解决方案
#1
11
Try this
Document doc = Document.parse( jsonlist.toString() );
#2
1
You can try inserting data in mongo using below sample code :-
您可以尝试使用以下示例代码在mongo中插入数据: -
String json = "{ 'name' : 'lokesh' , " +
"'website' : 'howtodoinjava.com' , " +
"'address' : { 'addressLine1' : 'Some address' , " +
"'addressLine2' : 'Karol Bagh' , " +
"'addressLine3' : 'New Delhi, India'}" +
"}";
DBObject dbObject = (DBObject)JSON.parse(json);
collection.insert(dbObject);
For your case you can Stringify your JSON object and then try inserting it into mongodb..
对于您的情况,您可以Stringify您的JSON对象,然后尝试将其插入mongodb ..
#1
11
Try this
Document doc = Document.parse( jsonlist.toString() );
#2
1
You can try inserting data in mongo using below sample code :-
您可以尝试使用以下示例代码在mongo中插入数据: -
String json = "{ 'name' : 'lokesh' , " +
"'website' : 'howtodoinjava.com' , " +
"'address' : { 'addressLine1' : 'Some address' , " +
"'addressLine2' : 'Karol Bagh' , " +
"'addressLine3' : 'New Delhi, India'}" +
"}";
DBObject dbObject = (DBObject)JSON.parse(json);
collection.insert(dbObject);
For your case you can Stringify your JSON object and then try inserting it into mongodb..
对于您的情况,您可以Stringify您的JSON对象,然后尝试将其插入mongodb ..