[英]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.我收到推文并使用 org.json.simple api 将字符串转换为对象。
JSONParser jsonParser = new JSONParser();
Object json = jsonParser.parse(in);
and I would like to insert the obj into couchdb using couchdb4j api我想使用 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);
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如何解决这个问题或者任何人都可以给出将json格式字符串或对象插入到couchdb中的解决方案
As the error said, couchdb may use net.sf.json.JSONObject. 正如错误所说,couchdb可能使用net.sf.json.JSONObject。
I've found an API in net.sf.json.JSONObject to convert a string to JSON object: 我在net.sf.json.JSONObject中找到了一个API,用于将字符串转换为JSON对象:
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. 这可能会有所帮助。
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? 如果你的问题是转换从
org.json.simple.Object
到net.sf.json.JSONObject
为什么不启动net.sf.json.JSONObject
摆在首位? 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);
将字符串转换为 net.sf.json.JSONObject
JSONObject json = JSONObject.fromObject(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.