JSONObject 转化成 DBObject

JSONObject 转化成 DBObject  的思路是把JSONObject 转化成String ,再转换成DBObject 或者BaicDBObject类型

一句话即可:

DBObject userDb = (DBObject) com.mongodb.util.JSON.parse(user
.toString());

示例如下:

import com.mongodb.DBObject;

import net.sf.json.JSONObject;

//JSONObect 转化成  DBJONObject
public class JSONObjectTotestDBObject {

    public static void main(String[] rags) {

        // 一个JSONObject类
        JSONObject user = new JSONObject();
        user.put("name", "xiaoA");
        user.put("age", 12);
        // 打印该类
        System.out.println(user);

        System.out.println("------------------------");
        // 转化
        DBObject userDb = (DBObject) com.mongodb.util.JSON.parse(user
                .toString());

        System.out.print(userDb);

    }

}

 

posted @ 2013-04-11 13:03  qgweizhan  阅读(3823)  评论(0编辑  收藏  举报