java如何将字符串转换为net.sf.json.JSONObject

use*_*908 5 java json couchdb

我收到推文并使用 org.json.simple api 将字符串转换为对象。

JSONParser jsonParser = new JSONParser();
Object json = jsonParser.parse(in);
Run Code Online (Sandbox Code Playgroud)

我想使用 couchdb4j api 将 obj 插入到 couchdb

 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);
Run Code Online (Sandbox Code Playgroud)

错误是:

   org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题或者任何人都可以给出将json格式字符串或对象插入到couchdb中的解决方案

bia*_*oqi 4

正如错误所述,couchdb 可能使用 net.sf.json.JSONObject。

我在 net.sf.json.JSONObject 中找到了一个将字符串转换为 JSON 对象的 API:

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

字符串原因没问题

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

这可能会有帮助。