we can add JSON directly to mongo using Java.
1. In the first example will add josn String directly as shown in the code below.
public static void jsonExmple(){
String jsonString = "{'Name' : 'James Smith', 'Age' : 29," +
"'Address' : {'area' : 'Area', 'city' : 'New York', 'country' : 'USA'} }";
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "testJson" );
DBCollection json = db.getCollection("json");
DBObject dbObject = (DBObject)JSON.parse(jsonString);
json.insert(dbObject);
DBCursor jsonDoc = json.find();
while (jsonDoc.hasNext()) {
System.out.println(jsonDoc.next());
}
}
the output on the mongo console would be like :
db.json.find().pretty();
{
"_id" : ObjectId("52c7fead91090c7db49d1604"),
"Name" : "James Smith",
"Age" : 29,
"Address" : {
"area" : "Area",
"city" : "New York",
"country" : "USA"
}
}
2. Another case would be, convert the Java object to json and add the json to mongoDB as shown below.
I have used the google json api to convert Java object to Json.
public static void jsonExample(){
MongoClient mongoClient = null;
try{
mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "testJson" );
DBCollection json = db.getCollection("json");
Person person = new Person();
person.setName("Jamse Cook");
person.setAge(29);
Address address = new Address();
address.setArea("Area");
address.setCity("New York");
address.setStreet("Aurthor Street");
address.setCountry("USA");
person.setAddress(address);
Gson gson = new Gson();
String jsonString = gson.toJson(person);
System.out.println(" print the json " + jsonString);
DBObject dbObject = (DBObject)JSON.parse(jsonString);
json.insert(dbObject);
}catch (Exception e) {
}
}
the output on the mongo console would be like :
{
"_id" : ObjectId("52c7fc479109fd33fdc45793"),
"name" : "Jamse Cook",
"age" : 29,
"address" : {
"street" : "Aurthor Street",
"area" : "Area",
"city" : "New York",
"country" : "USA"
}
}
1. In the first example will add josn String directly as shown in the code below.
public static void jsonExmple(){
String jsonString = "{'Name' : 'James Smith', 'Age' : 29," +
"'Address' : {'area' : 'Area', 'city' : 'New York', 'country' : 'USA'} }";
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "testJson" );
DBCollection json = db.getCollection("json");
DBObject dbObject = (DBObject)JSON.parse(jsonString);
json.insert(dbObject);
DBCursor jsonDoc = json.find();
while (jsonDoc.hasNext()) {
System.out.println(jsonDoc.next());
}
}
the output on the mongo console would be like :
db.json.find().pretty();
{
"_id" : ObjectId("52c7fead91090c7db49d1604"),
"Name" : "James Smith",
"Age" : 29,
"Address" : {
"area" : "Area",
"city" : "New York",
"country" : "USA"
}
}
2. Another case would be, convert the Java object to json and add the json to mongoDB as shown below.
I have used the google json api to convert Java object to Json.
public static void jsonExample(){
MongoClient mongoClient = null;
try{
mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "testJson" );
DBCollection json = db.getCollection("json");
Person person = new Person();
person.setName("Jamse Cook");
person.setAge(29);
Address address = new Address();
address.setArea("Area");
address.setCity("New York");
address.setStreet("Aurthor Street");
address.setCountry("USA");
person.setAddress(address);
Gson gson = new Gson();
String jsonString = gson.toJson(person);
System.out.println(" print the json " + jsonString);
DBObject dbObject = (DBObject)JSON.parse(jsonString);
json.insert(dbObject);
}catch (Exception e) {
}
}
the output on the mongo console would be like :
{
"_id" : ObjectId("52c7fc479109fd33fdc45793"),
"name" : "Jamse Cook",
"age" : 29,
"address" : {
"street" : "Aurthor Street",
"area" : "Area",
"city" : "New York",
"country" : "USA"
}
}
Thank you so much.
ReplyDelete