Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Escape character when serializing the dynamic object

Tags:

c#

json.net

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

like image 511
prasanthi Avatar asked Dec 12 '25 09:12

prasanthi


2 Answers

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"}
like image 140
Marc Gravell Avatar answered Dec 15 '25 08:12

Marc Gravell


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...

like image 29
David Zwart Avatar answered Dec 15 '25 09:12

David Zwart