Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.Net unexpected characters ("\") when serializing my entities

Tags:

I am using the excellent Json.Net library to serialize my entities generated by entity framework. I use the following code to do so :

using (MyVoucherEntities context = new MyVoucherEntities()) {   List<MyObject> list = context.MyObjects.ToList();   string json = JsonConvert.SerializeObject(list); } 

Everything goes well I mean, the objects are correctly serialized except one think : it adds escape characters "\" that makes me having nightmare when deserializing on the client side.

 [      {          \"$id\": \"1\",          \"CreationDate\": \"\\\/Date(1293186324257+0000)\\\/\",         \"ImageUrl\": \"http:\/\/www.google.com\",          \"Title\": \"Here is a title\"      } ] 

Does anybody know why and how I can get rid of these escape characters slash "\" ?

like image 709
Renaud Avatar asked Jan 18 '11 22:01

Renaud


People also ask

What does serializing JSON mean?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.

What is JSON serialization exception?

JsonSerializationException(String, String, Int32, Int32, Exception) Initializes a new instance of the JsonSerializationException class with a specified error message, JSON path, line number, line position, and a reference to the inner exception that is the cause of this exception.

Is JSON used for serialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

Can JSON serialize a list?

Json.NET has excellent support for serializing and deserializing collections of objects. To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.


1 Answers

I suspect it's not actually adding escape characters at all. I suspect you're just looking at the string in a debugger, and that's adding the escaping.

Try dumping it to a file or the console.

like image 90
Jon Skeet Avatar answered Sep 30 '22 14:09

Jon Skeet