I’m trying to make a call to a JSON ASP.NET web service with .NET.
It is ok when I send " { county : 'whatever' } ", but I get a 500 Internal Server error if I try for example " { county : 'It\'s ok' } “.
This is the code:
request.CookieContainer = container;
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
data = " { county : 'It\'s ok' } ";
buffer = Encoding.UTF8.GetBytes(data);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = buffer.Length;
request.Accept = "application/json, text/javascript, */*";
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(buffer, 0, buffer.Length);
// 500 Internal server error if i use { county : 'It\'s ok' }
response = (HttpWebResponse)request.GetResponse();
String ss;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
ss = sr.ReadToEnd();
I’ve found some posts with examples but I cannot make it work:
" { county : \"whatever\" } " and " { \"county\" : \"whatever\" } " also works.
But either of " { county : \"It's ok\" } ", " { county : \"It\'s ok\" } " or any other combination that contains a single quote in the variable work.
How can I send a single quote inside a JSON call?
Kind regards.
It’s because your example do not contain valid JSON.
You can use http://www.jsonlint.com to validate any JSON strings.
ALL keys and string values should be surrounded by double qoutes.
A valid JSON string would be: