Deserialize list of objects in C#

前端 未结 2 1178
温柔的废话
温柔的废话 2021-01-06 04:35

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":

相关标签:
2条回答
  • 2021-01-06 04:43

    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);
    
    0 讨论(0)
  • 2021-01-06 04:49

    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);
    }
    
    0 讨论(0)
提交回复
热议问题