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);

    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM