I get tweets and use org.json.simple api to convert a string to a object.
JSONParser jsonParser = new JSONParser();
Object json = jsonParser.parse(in);and I would like to insert the obj into couchdb using couchdb4j api
Session myDbSession = new Session("localhost",5984) Database myCouchDb = myDbSession.getDatabase("db-name"); Document newdoc = new Document(); Document newdoc = new Document(JSONObject json); myCouchDb.saveDocument(newdoc);The error is:
org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObjecthow to solve this problem or anyone can give a solution to insert a json format string or object into couchdb
13 Answers
As the error said, couchdb may use net.sf.json.JSONObject.
I've found an API in net.sf.json.JSONObject to convert a string to JSON object:
public static JSONObject fromObject( Object object ) { return fromObject( object, new JsonConfig() ); }
It's OK with String cause
else if( object instanceof String ){ return _fromString( (String) object, jsonConfig ); }
This may be help.
To convert String to net.sf.json.JSONObject
JSONObject json = JSONObject.fromObject(str); If your problem is with converting from org.json.simple.Object to net.sf.json.JSONObject why not start with net.sf.json.JSONObject in the first place? Using your code...
JSONObject json = JSONObject.fromObject(in);
Session myDbSession = new Session("localhost",5984)
Database myCouchDb = myDbSession.getDatabase("db-name");
Document newdoc = new Document();
Document newdoc = new Document(json);
myCouchDb.saveDocument(newdoc);