简体   繁体   中英

cannot parse json serialized string with mongo db object id as well as “\”

I have a controller, which will return an json serialized string in a Get operation:

data='[{"user":"CORP\\\\test","id":55cca31dd40238220c11d5a7}]'

However, in my angular JS layer, the json parse cannot work:

JSON.parse(data)
(program):1 Uncaught SyntaxError: Unexpected token c

I tried and turns out it is the returned string should be:

data='[{"user":"CORP\\\\\\\\test","id":"55cca31dd40238220c11d5a7"}]'

(there are two more \\, and there should be double quote for "id").

The strange thing is, I get the json string from .NET class:

var serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);

And, below is the model class:

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }

[BsonElement("User")]
public string User;

Anybody know how to resolve this issue to make the json parse in js code work?

要获得的ObjectID有周围的引号,我相信你需要的ID属性的类型更改为字符串显示在这个岗位

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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