We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi.
I'm using the MongoDB java driver to pull some data out of a MongoDB collection. I'm having problems converting the resultant DBObject into a processing JSON object. My code looks a little bit like this:
BasicDBObject query = new BasicDBObject("s__UID","123456789");
cursor = coll.find(query);
while(cursor.hasNext())
{
println(cursor.hasNext());
json = getJSONObject(cursor.next());
}
cursor.close();
The Println of cursor.hasNext works fine, but getJSONObject (or loadJSONObject) complain because I'm not passing in strings.
Can anyone help?
Answers
As an update, this thread also describes what I think is the same problem, but the link that PhiLho suggested as an answer which uses...
...Doesn't work for me, I get the error "The method parse(String) in the type JSONObject is not applicable for the arguments (DBObject)"
Jim
parse() is to convert a Json string to a Json Object. There is no automated way to convert an object of arbitrary complexity to Json (in Processing, at least; the Jackson library, for example, can do that).
So would anyone know how I could convert a DBObject into a string?
I think I've managed to make some progress, I've now got the DBobject into a String using:
String s = String.format("%s",cursor.next());
...and now I don't think it would be too difficult to get it into a JSONobject.
Jim
//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));
//result is a DbObject , output is a JSONObject