Deserialize json based on fields in .Net (C#)

后端 未结 5 615
余生分开走
余生分开走 2021-02-09 12:05

I\'m writing an app that gets a Json list of objects like this:

[
   {
       \"ObjectType\": \"apple\",
       \"ObjectSize\": 35,
       \"ObjectC         


        
5条回答
  •  南旧
    南旧 (楼主)
    2021-02-09 12:28

    Take a look at newtonsoft's JSON library

    With it you can do stuff like:

    ...
    public class Movie
    {
       public string Name;
       public DateTime ReleaseDate;
       public string[] Genres;
    }
    ......
    
    string json = @"{
      'Name': 'Bad Boys',
      'ReleaseDate': '1995-4-7T00:00:00',
      'Genres': [
        'Action',
        'Comedy'
      ]
    }";
    
    Movie m = JsonConvert.DeserializeObject(json);
    
    string name = m.Name;
    // Bad Boys
    

提交回复
热议问题