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));

Share:
12,865
Alaa-GI
Author by

Alaa-GI

Updated on June 04, 2022

Comments

  • Alaa-GI
    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
    Patrick Bell almost 8 years
    Answering questions without an explanation for why it is the answer isn't very helpful to the original poster.
  • Huy Hóm Hỉnh
    Huy Hóm Hỉnh almost 7 years
    Exactly code is: JSONObject output = new JSONObject(new JSON().serialize(result));
  • Chintan Panchal
    Chintan Panchal over 6 years
    This won't working for me, filling with null values.