How to handle single quote in the JSON string

Venkat 1 Reputation point
2021-08-24T23:53:51.363+00:00

I have created a class with list of properties where one of the property contains name which contains single quote. But when I serialize the object using JSON ( JsonConvert.SerializeObject) SerializeObjec and pass it to the Page.ClientScript.RegisterClientScriptBlock(.. where I am getting an error missing argument )

    customer.CustomerID = Convert.ToString(Session["Number"]);
    customer.CustomerName = Convert.ToString(Session["Name"]);
    customer.Type = type;
    customer.InvoiceFromDate = Convert.ToString(Session["From_Date"]);
    customer.InvoiceToDate = Convert.ToString(Session["To_Date"]);

    string output = JsonConvert.SerializeObject(customer);
    Page.ClientScript.RegisterClientScriptBlock(GetType(), "SaveInfo", "SetData('" + output + "','" + URL + "');", true);

In the above customer.CustomerName contains value = "The Young's Family"

Any help is appreciated

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Adrian CR 1 Reputation point
    2021-08-25T01:26:47.823+00:00

    Json don't allow single quotes, to fix the input you must change the character to double quote, for example str.replace(/'/g, '"')


  2. Timon Yang-MSFT 9,586 Reputation points
    2021-08-25T02:05:50.407+00:00

    Is there an exception in line 7?

    There is a single quote in my example, but I didn't encounter this problem.

        class Program  
        {  
            static void Main(string[] args)  
            {  
                MyClass myClass = new MyClass() { ID = 1, Name = "TimonYang's Name" };  
      
                var re = JsonConvert.SerializeObject(myClass);  
                MyClass myClass1 = JsonConvert.DeserializeObject<MyClass>(re);  
            }  
        }  
        class MyClass  
        {  
            public int ID { get; set; }  
            public string Name { get;set; }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Bruce (SqlWork.com) 61,731 Reputation points
    2021-08-25T02:20:25.557+00:00

    you issue is not the JSON which support unquoted single quotes, but that you render in a javascript string literal.

    for simplicity assume:

    var output = "The Young's Family";

    then

    "SetData('" + output +"')"
    

    renders as:

    SetData('The Young's Family')
    

    but because you used single quotes to the start the literal, you must quote:

       SetData('The Young\'s Family')
    

    therefore, in your output variable replace all "'" with "\'".

    output = output.Replace("'","\'"));

    note: need to escape \ in c#

    0 comments No comments