2013-03-16 36 views
0

我有一個小問題在這裏, 我想序列的一個字典,它包含stringintegersobjectnewtonsoft json.net跳過字符串或整數

現在我通過字典

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,並且事情在那裏變得混亂。

,所以我希望它跳過stringsintegers,但字典可以包含DateTime等等等等的東西。我只是指出了字符串和整數作爲例子。

字典包含我自己定製的類,entity classes,integers,stringsdate time等etc的東西。我只想JSON.NET序列化我自己的自定義類和實體類。

任何幫助,將不勝感激。

回答

0

您可以過濾字典由大會之前對其進行序列化,例如:

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