Deserialize JSON into Object C#

前端 未结 5 1595
广开言路
广开言路 2020-11-28 14:06

Hello I am in desperate need of some help. I have a json file, with an array of json objects. I cannot figure out how to deserialize it into a list of this object.

5条回答
  •  粉色の甜心
    2020-11-28 14:20

    This is very simple to do using Newtonsoft.JSON and there is a page in the documentation covering how to deserialize an object.

    Taken from the documentation page:

    public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList Roles { get; set; }
    }
    
    // code to deserialize from JSON string to a typed object
    string json = @"{
        'Email': 'james@example.com',
        'Active': true,
        'CreatedDate': '2013-01-20T00:00:00Z',
        'Roles': [
            'User',
            'Admin'
        ]
    ";
    
    Account account = JsonConvert.DeserializeObject(json);
    
    Console.WriteLine(account.Email);
    // james@example.com
    

提交回复
热议问题