简体   繁体   English

使用转义字符从单引号JS对象构造Mongo DBObject?

[英]Constructing a Mongo DBObject from single-quote JS object with escaped characters?

This question relates to the Java API / Driver, but may also be relevant to interfacing Mongo in other languages. 这个问题与Java API / Driver有关,但也可能与Mongo在其他语言中的接口有关。 I am new to Mongo and making some assumptions, so please correct me if I'm mistaken. 我是Mongo的新手并且做了一些假设,所以如果我弄错了,请纠正我。 A lot of documentation I'm finding, and references describe creating a DBObject given a structure like this: 我找到了很多文档,并且引用描述了在给定如下结构的情况下创建DBObject:

{
    foo: "bar",
    baz: {
        x : { lorem: "Ipsum" },
        y: { dolor : "sit amet" }
    }
}

Assume the above object exists (unchanged) in a file called foo.json . 假设上述对象在名为foo.json的文件中存在(未更改)。 Now, as I understand it, the content above is a JavaScript object literal (not valid JSON). 现在,据我所知,上面的内容是一个JavaScript 对象文字 (不是有效的JSON)。 Right? 对? However, this is the form referenced as "JSON" in the Mongo documentation. 但是,这是Mongo文档中引用为“JSON”的表单。

In any case, in my tests, I'm reading the foo.json file into a String and parsing it using (I believe) a fairly standard convention: 无论如何,在我的测试中,我正在将foo.json文件读入一个String并使用(我相信)一个相当标准的约定来解析它:

String fooString = readFile("foo.json");
Object o = com.mongodb.util.JSON.parse(fooString);
DBObject dbObj = (DBObject) o;

This works just fine. 这很好用。 Now, since foo.json isn't valid JSON, I assumed I'd be able to use a similar JavaScript object form: 现在,由于foo.json不是有效的JSON,我假设我能够使用类似的JavaScript对象形式:

{
    foo: 'bar',
    baz: {
        x : { lorem: 'Ipsum' },
        y: { dolor : 'sit amet' }
    }
}

Ok, fine, that seems to work. 好的,很好,这似乎有效。 Although, oddly enough in Mongo shell it appears to be stored with double quotes. 虽然,在Mongo shell中奇怪的是它似乎存储了双引号。 Since that works, I'm making another assumption that I'd be able to handle the JavaScript object form with escaped single quotes: 由于这有效,我正在做另一个假设,即我能够使用转义单引号处理JavaScript对象表单:

{
    foo: 'bar',
    baz: {
        x : { lorem: 'Ipsum.  Isn\'t working' },
        y: { dolor : 'sit amet' }
    }
}

However, when I try to parse this object (using com.mongodb.util.JSON.parse(fooString) ), a com.mongodb.util.JSONParseException is thrown. 但是,当我尝试解析此对象时(使用com.mongodb.util.JSON.parse(fooString) ),将抛出com.mongodb.util.JSONParseException Shouldn't all 3 forms be supported? 不应该支持所有3种形式吗?

Note: I'm using the org.mongodb:mongo-java-driver:mongo-java-driver:2.11.2 . 注意:我使用的是org.mongodb:mongo-java-driver:mongo-java-driver:2.11.2。

Thanks in advance for any/all help. 提前感谢任何/所有帮助。

JSON.parse is not a full-blown JSON parser, and it doesn't support escaped characters like that. JSON.parse不是一个成熟的JSON解析器,它不支持这样的转义字符。 If you want to manipulate JSON and objects, you're better off using something like MongoJack which was designed to work that way from the beginning. 如果你想操纵JSON和对象,你最好使用像MongoJack这样的东西,它从一开始就是这样工作的。

Strictly speaking in JSON all strings should be double quoted - see the json spec 严格地说,在JSON中, 所有字符串都应该是双引号 - 请参阅json规范

So the valid json document version would be: 所以有效的json文档版本将是:

{
    "foo": "bar",
    "baz": {
        "x": { "lorem": "Ipsum" },
        "y": { "dolor" : "sit amet" }
    }
}

Most JSON parsers handle some level of broken(ish) json but if you stick to spec it should work fine. 大多数JSON解析器处理某种程度的破坏(ish)json但是如果你坚持使用它,它应该可以正常工作。

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

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