How to encode the single quote/apostrophe in JSON.NET

前端 未结 4 1501
花落未央
花落未央 2021-02-08 18:55

How to encode the \' to \\u0027 with JSON.NET?

So that the json string would look something like this:

{\"Id\":8,\"CompanyName         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-08 19:26

    Newtonsoft.Json.JsonConvert.SerializeObject(your_object, Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonSerializerSettings { StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeHtml })

    is what I use when objects may contain an apostrophe, such as user names.

提交回复
热议问题