java如何将字符串转换为net.sf.json.JSONObject
我收到tweet并使用org.json.simple api将字符串转换为对象java如何将字符串转换为net.sf.json.JSONObject,java,json,couchdb,Java,Json,Couchdb,我收到tweet并使用org.json.simple api将字符串转换为对象 JSONParser jsonParser = new JSONParser(); Object json = jsonParser.parse(in); 我想使用couchdb4j api将obj插入couchdb Session myDbSession = new Session("localhost",5984) Database myCouchDb = myDbSession.getDatabas
JSONParser jsonParser = new JSONParser();
Object json = jsonParser.parse(in);
我想使用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);
错误是:
org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject
如何解决这个问题,或者任何人都可以给出将json格式的字符串或对象插入couchdb的解决方案,正如错误所说,couchdb可以使用net.sf.json.JSONObject 我在net.sf.json.JSONObject中找到了一个API,用于将字符串转换为json对象:
来自对象的公共静态JSONObject(对象对象){
返回fromObject(object,new JsonConfig());
}
这没关系,因为
else if(字符串的对象实例){
返回_fromString((String)对象,jsonConfig);
}
这可能会有所帮助。如果您的问题是从
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);
检查导入,确保正确使用库进行json解析,也就是说,如果使用的是json.simple,那么最好不要使用
net.sf.json.JSONObject
object。