Convert a DBObject to JSON
12,865
//assign the cursor to the DbObject
DbObject result= cursor.next();
//this line will convert the DbObject to JSONObject
JSONObject output = new JSONObject(JSON.serialize(result));
Author by
Alaa-GI
Updated on June 04, 2022Comments
-
Alaa-GI almost 2 years
I'm wondering how to parse DBObject to JSON, I'm using Jersey framework to create a restful service with Java, i want to load data from my MongoDB and convert it to JSON format and finaly display it in my web page. Here is what i tried to convert a DBObject that contains data to display, to a JSON object :
DBCursor cursor = dbUser.find(); JSON json = new JSON(); String dataUser = json.serialize(cursor);
But what i get it's a MongoDB format not a JSON, Do you have any idea please ?
-
Patrick Bell almost 8 yearsAnswering questions without an explanation for why it is the answer isn't very helpful to the original poster.
-
Huy Hóm Hỉnh almost 7 yearsExactly code is: JSONObject output = new JSONObject(new JSON().serialize(result));
-
Chintan Panchal over 6 yearsThis won't working for me, filling with null values.