3

This question may be duplicate, but I didn't find a satisfactory answer, so that's why I am raising the question.

I am working on serialization of dynamic objects. When I serialize a dynamic object, the API returns the response as

"{\"firstname\":\"prasanthi\",\"lastname\":\"kota\"}"

I didn't want to use string.Replace or RegexPattern. Is there any other way to do this?

I have tried JavaScriptSerializer, but it adds in quote marks with escape marks (\"). Here is my code:

    dynamic d = new ExpandoObject();
    d.firstname = "prasanthi";
    d.lastname = "kota"; 

   string serialized_info = JsonConvert.SerializeObject(d);

Update:

I am using serialized_info in another part of my code.I don't want slashes there. So, I want to remove slashes before.

I have tired which are mentioned in comments

dynamic x = new { firstname = "prasanthi", lastname = "kota" }; var serialized_info = JsonConvert.SerializeObject(x,Formatting.Indented);

this is displaying

"{\r\n  \"firstname\": \"prasanthi\",\r\n  \"lastname\": \"kota\"\r\n}"

I don't think this is the answer to my question. Can you suggest me in any other way to do other than string.replace

prasanthi
  • 562
  • 1
  • 9
  • 25
  • `serialized_info = serialized_info.Replace("\\",string.Empty);` – Aria Jan 02 '18 at 16:00
  • This shouldn't happened. Can you edit your question to include the full source code you have, which shows the problem and can be run by others? – Progman Jan 02 '18 at 16:04
  • I can't reproduce your issue... `dynamic x = new { firstname = "prasanthi", lastname = "kota" }; var serialized_info = JsonConvert.SerializeObject(x,Formatting.Indented);` – Hackerman Jan 02 '18 at 16:07
  • 5
    Are you sure your problem isn't [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179/3744182)? – dbc Jan 02 '18 at 16:08
  • 1
    I agree, this doesn't look like an issue. –  Jan 02 '18 at 16:10
  • 1
    @DavidZwart: thanks for your improvements to this question, good work. If you see chatty material (thanks in advance, please help me, I have been stuck for hours/days) you can just remove it, to save tidying it up. We excise chatty material fairly ruthlessly here. – halfer Jan 02 '18 at 22:19
  • After your update nothing changed to the question. It is not an issue. I think you are predictiing problems which will not occur. If you ARE running into parsing issues, then you should give an example where this will happen, since the issue is at that place and not here. – David Zwart Jan 08 '18 at 16:48

3 Answers3

5

There is no problem here. The \ is only appearing in the IDE as a debugging aid - representing the string visually in the same way that you would write it in C#. It doesn't actually contain the escape character. If you use:

Console.Write(serialized_info);

or:

File.WriteAllText(path, serialized_info);

then you will see normal correct JSON. The C# string literal:

"{\"firstname\":\"prasanthi\",\"lastname\":\"kota\"}"

is precisely the string with contents:

{"firstname":"prasanthi","lastname":"kota"}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

The actual problem is unclear. You want the string without escape marks? Do you want the string without quote marks? I'll address both.

  1. Without escape marks: Although you state that it is escaping backslashes, it is actually escaping quote marks as \" results in " when printing to a terminal (I edited your question). The escape mark makes sure the quote mark is correctly represented in a file, a debug terminal, etc. What you need to find out is if the escape mark disappears in at the goal destination (the place where the string is deserialized/parsed). I don't see why string.Replace(...) is unsuitable for this? Please explain this first, because it is your solution for now.
  2. Without quotes Quotes are necessary for String type serialized content. You can try to remove the backslashes with string.Replace(...), but this would remove the escaping feature, making it possible that the string is not recognized as such by the end-application. It could also throw errors when sending the data to your destination. I would strongly suggest keeping the quotes.

[EDIT following UPDATE] If you ARE running into parsing issues because of these escape marks, then you should give an example where this happens, since the issue is at that place and not here. Please close the question if no issue happens...

David Zwart
  • 431
  • 5
  • 23
  • Also take a look at https://stackoverflow.com/questions/20312974/newtonsoft-json-serializeobject-without-escape-backslashes – David Zwart Jan 02 '18 at 16:18
  • `string.Replace(...)` is unsuitable because if the value contains `\"` it will replace with `"` .For eaxmple `dynamic x = new { username= "prasanthi", password= "12452\" };`. In this case I may not be use the`string.Replace(...)`. – prasanthi Jan 03 '18 at 09:34
  • It is still applicable and your statement is wrong, because a slash character `"\"` will also be escaped with an escape mark: `"\\"`. That is the whole reason escape marks exist. – David Zwart Jan 08 '18 at 16:51
1

Have your API just return the object (ExpandoObject or object), or have it return an ActionResult and do return Json(d);

@dbc linked a full description question in the comments above: https://stackoverflow.com/a/25559255/356218

Thymine
  • 8,775
  • 2
  • 35
  • 47