Java how to convert string to net.sf.json.JSONObject

I get tweets and use org.json.simple api to convert a string to an object.

JSONParser jsonParser = new JSONParser();
Object json = jsonParser.parse(in);

and I would like to insert obj in 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);

Error:

   org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject

how to solve this problem, or can someone give a solution to insert a string or json format object in couchdb

+4
source share
2 answers

As stated in the error, couchdb can use net.sf.json.JSONObject.

I found the API in net.sf.json.JSONObject to convert a string to a JSON object:

public static JSONObject fromObject( Object object ) { return fromObject( object, new JsonConfig() ); }

Ok with the line

else if( object instanceof String ){ return _fromString( (String) object, jsonConfig ); }

It might be a help.

+1
source

org.json.simple.Object net.sf.json.JSONObject, net.sf.json.JSONObject ? ...

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

Source: https://habr.com/ru/post/1537440/


All Articles