java how to convert a string to net.sf.json.JSONObject

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.JSONObject

how to solve this problem or anyone can give a solution to insert a json format string or object into couchdb

1

3 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);

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like