views:

141

answers:

3

Hi, I'm using C# to send JSON to a PHP-Script, like this:

string json = "{";
json += "\"prop\":\"some text\"";
json += "}";

PostSubmitter post = new PostSubmitter();
post.Url = "http://localhost/synch/notein.php";
post.Type = PostSubmitter.PostTypeEnum.Post;
post.PostItems.Add("note", json);
post.Post();

Of course I'll have to escape the inner quotes, but they get sended to the script! To make things worse: There is text, which already has quotation marks, so those must be escaped to be valid JSON. In this case I want the backslashes to be transmitted. Any idea to accomplish this?

+2  A: 

Why not serialize the custom object to json result. That way you don't have to worry about the escaping, the framework would... Here is an example using JavaScriptSerializer - Convert objects to JSON in C# using JavaScriptSerializer

Svetlozar Angelov
A: 

Escape your backslashes \\:

json += "\\\"prop\":\\\"some text\\\"";
Oded
A: 

Ooops, thought PostSubmitter is from the .NET-framework, but it's third-party. Nevertheless: Turned out that this is a PHP-problem. If someone has a similar problem: Look for get_magic_quotes_gpc in PHP-docs.

Stefan N.