I Use JsonConvert to serialize an object and save it in a database.
This is a sample of the serialized string that I saved in database:
[{"matId":"1","value":"44"},{"matId":"2","value":"55"},{"matId":"4","value":"77"}]
Now when I get this string from database which has a lot of backslashes like this:
"[{\"matId\":\"1\",\"value\":\"44\"},{\"matId\":\"2\",\"value\":\"55\"},{\"matId\":\"4\",\"value\":\"77\"}]"
And for this reason I can't Deserialize
it.
.Replace("\\","")
method doesn't create any affect on this. I don't know why.
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.
You have to use JsonConvert.Deserialize
method.
Your json
string is wrapped within square brackets ([])
, hence it is interpreted as array. Therefore, you need to deserialize
it to type
collection of one class
, for example let's call it MyClass
.
public class MyClass
{
public int matId { get; set; }
public int value { get; set; }
}
Here is Deserialize
method.
var results=JsonConvert.DeserializeObject<List<MyClass>>(json);
Backslashes represent serialized object. You need to deserialize your List object. You can try using Generics:
public List<T> Deserialize<T>(string path)
{
return JsonConvert.DeserializeObject<List<T>>(path);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With