Issue
I have a JSON object obj which I want to store into MongoDB:
JSONObject obj = new JSONObject();
obj.put("title", obj1.title);
//For rules:
Map m = new LinkedHashMap();
// for rules, first create JSONArray
JSONArray ja = new JSONArray();
m = new LinkedHashMap();
m.put("right_connective", "&&");
m.put("attribute", "amount");
m.put("operator", "<=");
m.put("value", obj1.amount);
m.put("rank", 1);
m.put("encapsulated", "false");
ja.add(m);
m = new LinkedHashMap();
m.put("left_connective", "&&");
m.put("right_connective", "&&");
m.put("attribute", "project");
m.put("operator", "==");
m.put("value", obj1.project);
m.put("rank", 2);
m.put("encapsulated", "false");
ja.add(m);
m = new LinkedHashMap();
m.put("left_connective", "&&");
m.put("right_connective", "&&");
m.put("attribute", "type");
m.put("operator", "==");
m.put("value", obj1.type);
m.put("rank", 3);
m.put("encapsulated", "false");
ja.add(m);
m = new LinkedHashMap();
m.put("left_connective", "&&");
m.put("attribute", "car");
m.put("operator", "==");
m.put("value", obj1.car);
m.put("rank", 4);
m.put("encapsulated", "false");
ja.add(m);
obj.put("rule", ja);
Document doc = Document.parse( obj.toString() );
BasicDBObject dbObject = mapper.readValue(obj, BasicDBObject.class);
collection.insert(dbObject);
I'm getting an error that "mapper cannot be resolved to a type". Which import statement do I need to include? Otherwise, can you suggest alternate methods? I don't want to go the MongoDocument route since the insertOne() function is skipping some documents while writing to DB.
Solution
You don't need a mapper if you are working with org.json.JSONObject
:
DBObject object = (DBObject) JSON.parse(obj.toString());
collection.insert(object)
Answered By - ilopezluna
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.