使用JSON序列蒙戈DB对象ID反序列化 [英] Mongo DB object Id deserializing using JSON serializer
问题描述
VAR docToJson = doc.ToJson< BsonDocument>();
故事精选= JsonConvert.DeserializeObject<故事>(docToJson);
公共类的故事
{
[JsonProperty(_ ID),JsonConverter(typeof运算(ObjectIdConverter))]
公众的ObjectId标识{搞定;组; }
....公共类ObjectIdConverter:JsonConverter
{
公共覆盖无效WriteJson(JsonWriter作家,对象的值,JsonSerializer串行)
{
serializer.Serialize(作家,value.ToString());
} 公众覆盖对象ReadJson(JsonReader读者,类型的objectType,对象existingValue, JsonSerializer串行)
{
JToken令牌= JToken.Load(读卡器);
返回新的ObjectId(token.ToObject<串GT;());
} 公众覆盖布尔CanConvert(类型的objectType)
{
返回(==的objectType typeof运算(的ObjectId));
}
}
}
我坚持我已经尝试了半打的方法,我仍然得到使用JSON读者,任何想法的人同样的错误?
最后试过这个从<一个href=\"http://stackoverflow.com/questions/19097872/json-net-deserializing-mongo-objectid-is-giving-the-wrong-result\">SO*
JsonReader异常
在解析值意外遇到性格:O.路径_id,1号线,10位
块引用>JSON字符串看起来是这样的:
{
_id:的ObjectId(5378f94a3513fa3374be7e20),
CC:国标,
username的:XYZ,
userImage:IMG / 16.jpg,
createdDate:ISODate(2014-05-18T18:17:46.983Z),
Headling:Veniam,阿梅特,incidunt veniam,ipsam nostrud了Natus exercitationem consectetur,EOS dolorem。
副标题:Veniam,阿梅特,incidunt veniam,ipsam nostrud。
}
解决方案您收到此错误,因为对于
_id
属性的值不符合JSON标准(看到 JSON.org )。 JSON值必须是下列之一:
- 一个字符串(开始和引号结束
),
- 一个数字,
- 的对象(启动和用花括号结束
{
和}
),- 一个数组(开始和方括号结束
[
和]
)或- 关键字
真正
,假
或空
值
的ObjectId(5378f94a3513fa3374be7e20)
看起来是一个函数,它是无效的。该值ISODate(2014-05-18T18:17:46.983Z)
有同样的问题。您将需要以某种方式改变你的JSON达到标准,如果你想使用JSON.net解析它。var docToJson = doc.ToJson<BsonDocument>(); story Featured = JsonConvert.DeserializeObject<story>(docToJson); public class story { [JsonProperty("_id"), JsonConverter(typeof(ObjectIdConverter))] public ObjectId Id { get; set; } .... public class ObjectIdConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, value.ToString()); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); return new ObjectId(token.ToObject<string>()); } public override bool CanConvert(Type objectType) { return (objectType == typeof(ObjectId)); } } }
I'm stuck I've tried half a dozen methods, I'm still getting the same error with json reader, any ideas anyone?
Last tried this from SO*
JsonReader Exception
Unexpected character encountered while parsing value: O. Path '_id', line 1, position 10.
The JSON string looks like this:
{ "_id": ObjectId("5378f94a3513fa3374be7e20"), "cc": "GB", "userName": "xyz ", "userImage": "img/16.jpg", "createdDate": ISODate("2014-05-18T18:17:46.983Z"), "Headling": "Veniam, amet, incidunt veniam, ipsam nostrud natus exercitationem consectetur, eos dolorem. ", "subheading": "Veniam, amet, incidunt veniam, ipsam nostrud. " }
解决方案You are getting this error because the value for the
_id
property does not conform to the JSON standard (see JSON.org). JSON values must be one of the following:
- a string (starts and ends with quote marks
"
),- a number,
- an object (starts and ends with curly braces
{
and}
),- an array (starts and ends with square brackets
[
and]
), or- the keywords
true
,false
, ornull
.The value
ObjectId("5378f94a3513fa3374be7e20")
appears to be a function, which is not valid. The valueISODate("2014-05-18T18:17:46.983Z")
has the same problem. You will need to somehow change your JSON to meet the standard if you want to parse it using JSON.net.这篇关于使用JSON序列蒙戈DB对象ID反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!