newtonsoft json.net跳过字符串或整数

时间:2013-03-16 13:34:20

标签: c# json serialization json.net

我在这里遇到一点问题, 我想序列化一个词典,它包含stringintegersobject

现在我在循环词典

 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序列化该词典中的stringintegers。因为很少有字符串包含\r\n,而且事情变得混乱了。

所以我希望它跳过stringsintegers,但字典可能包含DateTime等等。我只指出字符串和整数作为例子。

字典包含我自己的自定义类,entity classesintegersstringsdate time等等。我只希望JSON.NET序列化我自己的自定义类和实体类。

任何帮助将不胜感激。

1 个答案:

答案 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);