string
,integers
,object
现在我在循环词典
var data ="";
foreach (var dict in dictObject)
{
var value = JsonConvert.SerializeObject(dict.value, Formatting.Indented,new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Converters = new List<JsonConverter>
{
new IsoDateTimeConverter(){DateTimeFormat = "yyyy-MM-dd hh:mm:ss"}
}
});
data += dict.key +"="+ value;
}
现在我不希望Json.Net
序列化该词典中的string
,integers
。因为很少有字符串包含\r\n
,而且事情变得混乱了。
所以我希望它跳过strings
和integers
,但字典可能包含DateTime
等等。我只指出字符串和整数作为例子。
字典包含我自己的自定义类,entity classes
,integers
,strings
,date time
等等。我只希望JSON.NET
序列化我自己的自定义类和实体类。
任何帮助将不胜感激。
答案 0 :(得分:0)
您可以在序列化之前过滤字典,例如通过Assembly:
var customTypesAssembly = typeof(CustomClass).Assembly;
var filteredDictionary = dictionary.Where(x => x.Value.GetType().Assembly == customTypesAssembly)
.ToDictionary(x => x.Key, x => x.Value);
var json = JsonConvert.SerializeObject(filteredDictionary);