4

I'm using System.Web.Script.Serialization.JavaScriptSerializer() to serialize dictionary object into JSON string. I need to send this JSON string to API sitting in the cloud. However, when we serialize it, serializer replaces all the double quotes with \"

For example -

Ideal json_string = {"k":"json", "data":"yeehaw"}

Serializer messed up json_string = {\"k\":\"json\",\"data\":\"yeehaw\" }

Any idea why it is doing so? And I also used external packages like json.net but it still doesn't fix the issues.

Code -

Dictionary<string, string> json_value = new Dictionary<string, string>();
json_value.Add("k", "json");
json_value.Add("data", "yeehaw");
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json_string = jsonSerializer.Serialize(json_value);
Adam
  • 41
  • 1
  • 4
  • Please show the code you're using, the actual values before serialization, and how you're seeing the serialized data (e.g. in a debugger) – Jon Skeet Feb 27 '12 at 09:11
  • Thanks for your commend. I've updated my question with code. :) – Adam Feb 27 '12 at 09:25
  • You still haven't told us how you're looking at the serialized data. – Jon Skeet Feb 27 '12 at 09:29
  • I'm looking at the data via debugger (quick watch) – Adam Feb 27 '12 at 09:36
  • Right, then Marc is spot on. You *don't* actually have those backslashes, and the problem lies elsewhere. – Jon Skeet Feb 27 '12 at 09:38
  • @user1235159 which is exactly what I said - you're looking in the IDE. The **value** is what you wanted; simply, it is using the c# representation of that value for the reasons I already gave. The *value*, however, is exactly what you wanted. The slashes don't actually exist. – Marc Gravell Feb 27 '12 at 09:38

3 Answers3

7

I'm going to hazard the guess that you're looking in the IDE at a breakpoint. In which case, there is no problem here. What you are seeing is perfectly valid JSON; simply the IDE is using the escaped string notation to display it to you. The contents of the string, however, are your "ideal" string. It uses the escaped version for various reasons:

  • so that you can correctly see and identify non-text characters like tab, carriage-return, new-line, etc
  • so that strings with lots of newlines can be displayed in a horizontal-based view
  • so that it can be clear that it is a string, i.e. "foo with \" a quote in" (the outer-quotes tell you it is a string; if the inner quote wasn't escaped it would be confusing)
  • so that you can copy/paste the value into the editor or immediate-window (etc) without having to add escaping yourself
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • No, when I sent my JSON to API, it errors out :) But I will take a look at it. Thanks – Adam Feb 27 '12 at 09:28
  • @user1235159: I suspect Marc is right about this (which is why I asked the question about how you were looking at the data) and you're actually seeing a completely *different* error. – Jon Skeet Feb 27 '12 at 09:30
  • @user1235159 to clarify - I've just run your example; the JSON that comes out is exactly what you wanted: `{"k":"json","data":"yeehaw"}`. If I look at it in the IDE, it is of course displayed as `{\"k\":\"json\",\"data\":\"yeehaw\"}`, but that doesn't change that it **is** the value you wanted. – Marc Gravell Feb 27 '12 at 09:35
  • I'm looking at the data via debugger (quick watch) – Adam Feb 27 '12 at 09:36
  • ohh..so its IDE formatting issue. Dang! I'm doing something wrong then. Thanks Marc and Jon. I sincerely appreciate it. – Adam Feb 27 '12 at 09:39
  • @user1235159 Exactly. Those slashes don't actually exist; that is simply an artefact of having to display a string in a way that is most useful to the reader. – Marc Gravell Feb 27 '12 at 09:39
2

Make sure you're not double serializating the object. It happened to me some days ago.

GLlompart
  • 261
  • 5
  • 18
  • Thank you for pointing out such a rookie mistake that one can easily overlook, I for one surely did so. – UNOPARATOR Jun 13 '21 at 15:11
  • Thank you for this. On my Azure Function code, I was Serializing and then doing return new JsonResult which was serializing it again. – Ralms Jul 28 '21 at 00:50
0

What you're seeing is a escape character

Your JSON is a String and when you want to have " in a string you must use one of the following:

string alias = @"My alias is ""Tx3""";

or

string alias = "My alias is \"Tx3\"";

Update

Just to clarify. What I wanted say here is that your JSON is perfectly valid. You're seeing the special characters in the IDE and that is perfectly normal like Jon & Marc are pointing in their answers and comments. Problem lies somewhere else than those \ characters.

Tx3
  • 6,796
  • 4
  • 37
  • 52
  • Thanks for your answer. Where should I add @? Before serialization? I'm unclear on that. Thanks once again. I've also updated my question with code. – Adam Feb 27 '12 at 09:26