16

I want to convert the following json structure to BasicDBOject in java and insert into mongo db.

My JSON structure is

{
    "it": {
        "batch": "2013",
        "students": [
            {
                "name": "joe"
            },
            {
                "name": "john"
            }
        ]
    }
}
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
user2189941
  • 775
  • 2
  • 7
  • 10

2 Answers2

32

com.mongodb.util.JSON has a parse method.

BasicDBObject implements DBObject

Object o = com.mongodb.util.JSON.parse("Your JSON structure or JSONObj.toString()");
DBObject dbObj = (DBObject) o;
Alper
  • 12,860
  • 2
  • 31
  • 41
6
com.mongodb.util.JSON.parse 

Is deprecated

After version 3.6.1 use:

String json = "{"name": "joe"}";
Object o = BasicDBObject.parse(json);

Follow here: deprecated-list

Fabricio
  • 366
  • 6
  • 20