Escape Quote in C# for javascript consumption

后端 未结 9 1244
天涯浪人
天涯浪人 2020-11-27 17:24

I have a ASP.Net web handler that returns results of a query in JSON format

public static String dt2JSON(DataTable dt)
{
    String s = \"{\\\"rows\\\":[\";
         


        
9条回答
  •  没有蜡笔的小新
    2020-11-27 18:02

    Well, for starters you do not need quotes around the keys.

    {rows:[,]} is valid.
    
    and you could dt.Table.Columns[i].ToString().Replace("\","")
    

    But if you want to retain the double quotes, single quote works the same way double quotes do in JS

    Otherwise you could do

    String.Format("{name: \"{0}\"}",Columns[i].ToString().Replace("\",""))
    

提交回复
热议问题