简体   繁体   English

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

[英]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.Objectnet.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.

相关问题 java.lang.ClassNotFoundException: net.sf.json.JSONObject - java.lang.ClassNotFoundException: net.sf.json.JSONObject 转义为net.sf.json.JSONObject - Escape for net.sf.json.JSONObject 循环调用Google Maps API以对数十个地址进行地理编码并获取“ java.lang.String无法转换为net.sf.json.JSONObject” - Call Google Maps API in a loop to geocode dozens of addresses and get “java.lang.String cannot be cast to net.sf.json.JSONObject” 将net.sf.json.JSONObject与Google地理编码一起使用时获取java.lang.ClassCastException - Getting a java.lang.ClassCastException when using net.sf.json.JSONObject with Googles Geocoding net.sf.json.JSONObject会在不期望的地方添加反斜杠吗? - net.sf.json.JSONObject adds backslashes where it's not expected? java.lang.ClassCastException: 类 net.sf.json.JSONObject 不能转换为类 net.sf.json.JSONArray - java.lang.ClassCastException: class net.sf.json.JSONObject cannot be cast to class net.sf.json.JSONArray 如何将具有空值的密钥放在net.sf.json.JSONObject中? - How can I put key with null value in net.sf.json.JSONObject? 如何在Java中将无效的JSON字符串转换为JSONObject? - How to convert Invalid JSON String to JSONObject in Java? 如何使用net.sf.json在Java中生成JSON字符串? - How to generate JSON string in Java using net.sf.json? 如何转换清单 <JSONObject> 到Java中的Json String(com.amazonaws.util.json.JSONObject) - How to convert List <JSONObject> to Json String in java (com.amazonaws.util.json.JSONObject)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM