2012-12-31 119 views

回答

6

也許這個例子可以幫助

string obj = "abcn\n\rüö&/<>"; 
Console.WriteLine(Serialize(obj, StringEscapeHandling.Default)); 
Console.WriteLine(Serialize(obj, StringEscapeHandling.EscapeHtml)); 
Console.WriteLine(Serialize(obj, StringEscapeHandling.EscapeNonAscii)); 

public static string Serialize(object o,StringEscapeHandling stringEscapeHandling) 
{ 
    StringWriter wr = new StringWriter(); 
    var jsonWriter = new JsonTextWriter(wr); 
    jsonWriter.StringEscapeHandling = stringEscapeHandling; 
    new JsonSerializer().Serialize(jsonWriter,o); 
    return wr.ToString(); 
} 
+1

只要按下了同樣的問題。有沒有辦法使用標準的JsonConvert.Serialize對象來保存其他設置? – chester89

+0

@ chester89是的。這是Settings對象的一個​​選項** settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml **,您可以將它傳遞給JsonConvert()。 –