Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java how to convert a string to net.sf.json.JSONObject

Tags:

java

json

couchdb

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

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

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

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

like image 710
user2829908 Avatar asked Apr 22 '14 01:04

user2829908


1 Answers

As the error said, couchdb may use net.sf.json.JSONObject.

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

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.

like image 132
biaobiaoqi Avatar answered Oct 24 '22 22:10

biaobiaoqi