带有.net的Json序列化问题 [英] Json Serialization problem with .net

查看:64
本文介绍了带有.net的Json序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有C#的Newtonsoft json V 6.0.0我正在使用json序列化对象。



我的对象包含各种类型的集合,我在序列化对象之后完成所有搜索并获得响应。即使我得到例外



System.InvalidOperationException:集合被修改;枚举操作可能无法执行。

在System.Collections.Generic .List`1.Enumerator.MoveNextRare()

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .Serialize(JsonWriter jsonWriter,Object value,Type objectType)

at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType)

at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value,Type type,JsonSerializer jsonSerializer)





我无法跟踪在序列化期间修改的对象。在我的代码序列化中以高频率在线程中运行。



我的代码是

i am using Newtonsoft json V 6.0.0 with C# i am serializing object using json.

My object contains various type of collection and i am serializing object after finish all search and getting response. Even i am getting exception

"System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)"


I am not able to track the object which is modifying during serialization. in my code serialization is running in thread in high frequency.

my code is

var Jsonsettings = new JsonSerializerSettings
           {
               NullValueHandling = NullValueHandling.Ignore,
               DefaultValueHandling = DefaultValueHandling.Ignore,
               ContractResolver = new WBContractResolver()
           };





var SerializedJson = JsonConvert.SerializeObject(this,Jsonsettings);



提前致谢!!!



var SerializedJson = JsonConvert.SerializeObject(this, Jsonsettings);

Thanks in advance !!!


推荐答案

错误消息的第一行表示在序列化期间修改了集合。您是否使用循环在序列化期间从此集合中删除项目?或者是否有另一个线程从集合中删除项目?
The first line of the error message says that the collection was modified during serialization. Do you use a loop that removes items from this collection during serialization? Or is there another thread removing items from the collection?


这篇关于带有.net的Json序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆