开发者

Trim double quotes in json received from mongoDB

I am using JSON and MongoDb for the first time.. So when i insert into Mongodb and retrieve the result I get the value as

开发者_运维知识库
{ "_id" : { "$oid" : "4e67eb04b475e7c236a0c82c"} , "database" : "mkyongDB" , "table" : "hosting" , "detail" : { "records" : 99 , "index" : "vps_index1" , "active" : "true"}}

When i exrtact the value of "database" i get the value as

"mykongDB"

With double quotes displayed. And i have to remove the double quotes every time i extract the value from JSON. Is this the way JSON or MongoDB behaves or am i missing something..

My code to retrieve value using mongo driver in java

public DBObject find(String key,String value){
    BasicDBObject query = new BasicDBObject(key,value);
    return collection.findOne(query);
}

The way i extract value from JSON is

    DBObject dboject = client.find("database", "mkyongDB");
    System.out.println(dboject);
    JsonNode node =  mapper.readTree(dboject.toString());
    JsonNode innerNode = ((ObjectNode)node).get("_id");
    System.out.println(innerNode);
     String objectId = innerNode.get("$oid").toString();

I am using jackson JSON parser

whats wrong ? I dont want to handle removing the double quotes every time i extract the value.

Thanks


It doesn't look like the double quotes are being stored in the Mongo field as part of the string. If they were, the JSON output would look something like this:

{ "database" : "\"mkyongDB\"" }

So, it's probably a result of how the JSON is being parsed.

Does the Java driver deserialise the JSON into the DBObject class? Maybe you access the field by calling something like dboject.get("database") and casting the result to a string?

It's also worth checking out the Java Language Center, there are loads of links to tutorials, libraries and frameworks for POJO mapping that might make life easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜