0

I've been having difficulty for a long time with json. I have a soap service (asmx) that returns json for use in the Datatables and fullcalendar js plugins. I send a paramtised query to sql which fills a dataset. I then foreach through the dataset, append a few bits based on ifs, and populate a List<>. I then do the following on the List: string json = JsonConvert.SerializeObject(Events); The issue that i have, is that if the title of my event, has ab obscure character, like é,”,ô etc, the plugins fail parsing the json, as the json is malformed. The only way i've been able to get around this, is by writing a dirty method, to string replace the title before it is serialized. i.e

   try
            {
                SqlConnection con = new SqlConnection(constring);
                SqlDataAdapter DA = new SqlDataAdapter();
                DataSet ds = new DataSet();
                List<CalendarEvent> pdetails = new List<CalendarEvent>();
                using (con)
                {
                    using (SqlCommand cmd = new SqlCommand("[recmgt].[getCalendarEntries]", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        con.Open();
                        DA.SelectCommand = cmd;
                        DA.Fill(ds);
                        con.Close();
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            CalendarEvent per = new CalendarEvent();
                            per.id = dr["ID"].ToString();
                            per.Title = cleanTitle(dr["Title"].ToString());                                                   
                            per.Starts = dr["Starts"].ToString();
                            per.Ends = dr["Ends"].ToString();
                            pdetails.Add(per);
                        }
                    }
                }
                DA.Dispose();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                serializer.MaxJsonLength = Int32.MaxValue;
                string strResponse = serializer.Serialize(pdetails);
                Context.Response.Clear();
                Context.Response.ContentType = "application/json";
                Context.Response.AddHeader("content-length", strResponse.Length.ToString());
                Context.Response.Flush();
                Context.Response.Write(strResponse);
            }

            catch (Exception exc)
            {
            }
}

  public string cleanTitle(string PremTitle)
        {
            PremTitle = PremTitle.Replace("é", "e");
            PremTitle = PremTitle.Replace("/", "_");
            PremTitle = PremTitle.Replace("'", "\'");
            PremTitle = PremTitle.Replace("’", "\'");
            PremTitle = PremTitle.Replace("“", "\'");
            PremTitle = PremTitle.Replace("”", "\'");
            PremTitle = PremTitle.Replace(@"\", " ");
            PremTitle = PremTitle.Replace("–", "-");
            PremTitle = PremTitle.Replace("—", "-");
            PremTitle = PremTitle.Replace("£", "");
            PremTitle = PremTitle.Replace("ô", "o");
            return PremTitle;
        }

I feel like i'm firefighting a bit, everytime i get a datatable failure, i dig around in sql to find the char causing it, and add it to the method above. Be great if someone could point me in the right direction, or some method to return ascii instead of chars? Many thanks :)

  • In your question you show the workaround code, but can you please [edit] your question to show a [mcve], specifically the simplest possible code to generate the malformed JSON that you don't want? Using hardcoded inputs is fine as long as the resulting JSON demonstrates the problem. – dbc Aug 25 '21 at 15:39
  • Possibly some encoding other than utf8 is expected by the server. If so setting [`JsonSerializerSettings.StringEscapeHandling`](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_StringEscapeHandling.htm) to `StringEscapeHandling.EscapeNonAscii` or `StringEscapeHandling.EscapeHtml` might work around the problem. – dbc Aug 25 '21 at 16:34
  • See e.g. [escape accented chars on utf-8 json](https://stackoverflow.com/q/29899386/3744182), [How to serialize special characters with JsonSerializing](https://stackoverflow.com/q/50409273/3744182) or [How to encode the single quote/apostrophe in JSON.NET](https://stackoverflow.com/q/13541998/3744182). – dbc Aug 25 '21 at 16:34
  • 1
    @dbc many thanks for pointing me in the right direction. This is now resolved! Not sure how to mark a question as answered though. – thebigsc Aug 26 '21 at 07:47
  • 1
    @thebugsc - if the answer was to use `StringEscapeHandling.EscapeNonAscii` we could mark it as a duplicate of [How to serialize special characters with JsonSerializing](https://stackoverflow.com/q/50409273/3744182). If not a duplicate you could make [self answer](https://stackoverflow.com/help/self-answer). – dbc Aug 26 '21 at 14:31

0 Answers0