简体   繁体   English

Mongodb- Java函数com.mongodb.util.JSON.parse()

[英]Mongodb- java function com.mongodb.util.JSON.parse()

I am now using java method com.mongodb.util.JSON.parse(param) to convert my json string to a DBObject.I find that, in most cases , for example , when the key in the json string is simple type like string or int ,it can be used by both JSON.parse() method and terminal. 我现在使用java方法com.mongodb.util.JSON.parse(param)将我的json字符串转换为DBObject。我发现,在大多数情况下,例如,当json字符串中的键是简单类型如string时或int,它可以被JSON.parse()方法和终端使用。 For example: 例如:

json string:{'times':8}

Both JSON.parse({'times':8}) and db.collection.find({'times':8}) in terminal can work correctly. 终端中的JSON.parse({'times':8})和db.collection.find({'times':8})都可以正常工作。

But when I do a query on ISODate or _id,things become different: 但是,当我对ISODate或_id进行查询时,事情变得不同了:

json string 1 :{'createDate':ISODate('2013-10-21T06:39:16.692Z')} 

json strign 2: {'createDate':{'$date':'2013-10-21T06:39:16.692Z'}}

json string 1 can work correctly in terminal but cannot be parsed by JSON.parse() method. json字符串1可以在终端中正常工作,但无法通过JSON.parse()方法进行解析。 On the contrary , json string 2 can be parsed by JSON.parse() method but cannot be used in terminal. 相反,JSON字符串2可以通过JSON.parse()方法进行解析,但不能在终端中使用。

The same thing happened in _id. _id中发生了同样的事情。

json string 3:{'_id':ObjectId('1231daf213432414321431')} 

json string 4:{'_id':{'$oid':'fadf234234sdfadfasdfa12'}}

json string 3 can work correctly in terminal but cannot be parsed by JSON.parse() method. json字符串3可以在终端中正常工作,但无法通过JSON.parse()方法进行解析。 On the contrary , json string 4 can be parsed by JSON.parse() method but cannot be used in terminal. 相反,json字符串4可以通过JSON.parse()方法进行解析,但不能在终端中使用。 I don't know the reason of these differences. 我不知道这些差异的原因。

The constructors you are using in the Mongo Shell are JavaScript code that is obviously not part of strict JSON that you want to parse in your Java application. 在Mongo Shell中使用的构造函数是JavaScript代码,这显然不是要在Java应用程序中解析的严格JSON的一部分。 The point to be criticized here is rather that the shell does not support something like '$date' (as I have just tested myself as well). 这里要批评的是,shell不支持“ $ date”之类的东西(因为我也已经对自己进行了测试)。

This is for example discussed here . 例如, 这里讨论。

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

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