mongodb转换List实体(去掉多余的字段) 批量添加

时间:2024-01-14 09:51:20
       Gson gson = new Gson();

      String str = "list集合json字符串";
            JsonObject object = gson.fromJson(str, JsonObject.class);
List<实体> t = gson.fromJson("字符串",
new TypeToken<List<实体>>() {
}.getType());
BasicDBList basicDBList = (BasicDBList) JSON.parse(gson.toJson(t));
DBObject object2 = new BasicDBObject("list", basicDBList);
List<DBObject> list = (List<DBObject>) object2.get("list");

单个对象的:

      Gson gson = new Gson();
实体 t = gson.fromJson("json字符串",
实体.class);
DBObject object = (DBObject) JSON.parse(gson.toJson(t));

如有更好的方法欢迎指点