How to send a single quote as value in JSON webservice call
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\" 开发者_如何学C} "
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 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:
{ "county" : "It's ok" }
{ "county" : "'" } // what's wrong with this?
精彩评论