简体   繁体   English

如何将DBObject的内容转换为字符串?

[英]How to convert contents of DBObject to a string?

Printing the DBObject through following code: 通过以下代码打印DBObject:

DBObject theObj = cursor.next();
System.out.println(theObj);

Output: 输出:

{ "_id" : { "$oid" : "58b94b7bcd4b42b0b5e8c7c2"} , "content" : "<p><del class=\"ice-del ice-cts\" data-changedata=\"\" data-cid=\"2\" data-last-change-time=\"1488543686518\" data-time=\"1488543686518\" data-userid=\"\" data-username=\"\">Old content</del></p>\r\n"}

Now, consider the following variable 现在,考虑以下变量

String content;

I need the value of the string variable content to be content field value in the database "<p><del class=\\"ice-del ice-cts\\" data-changedata=\\"\\" data-cid=\\"2\\" data-last-change-time=\\"1488543686518\\" data-time=\\"1488543686518\\" data-userid=\\"\\" data-username=\\"\\">Old content</del></p>\\r\\n" 我需要将字符串变量content的值设置为数据库"<p><del class=\\"ice-del ice-cts\\" data-changedata=\\"\\" data-cid=\\"2\\" data-last-change-time=\\"1488543686518\\" data-time=\\"1488543686518\\" data-userid=\\"\\" data-username=\\"\\">Old content</del></p>\\r\\n"内容字段值"<p><del class=\\"ice-del ice-cts\\" data-changedata=\\"\\" data-cid=\\"2\\" data-last-change-time=\\"1488543686518\\" data-time=\\"1488543686518\\" data-userid=\\"\\" data-username=\\"\\">Old content</del></p>\\r\\n"

How to do that? 怎么做? I am not able to find googling. 我找不到谷歌搜索。

Solution: 解:

DBObject theObj = cursor.next();
String contentString =  theObj.getString("content");
System.out.println(contentString);

Output: 输出:

<p><del class="ice-del ice-cts" data-changedata="" data-cid="2" data-last-change-time="1488543686518" data-time="1488543686518" data-userid="" data-username="">Old content</del></p>

I think that this should be more efficient, and I'm pretty sure that is gonna work: 我认为这应该更有效,并且我很确定这会起作用:

DBObject theObj = cursor.next();
String result = theObj.toString();
System.out.println(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM