Deserializing JSON into string array

后端 未结 2 2008
醉话见心
醉话见心 2021-01-05 03:22

I just started dabbling with C#, and I\'ve been banging my head over JSON deserialization for a while now. I\'m using Newtonsoft.Json library. I\'m expecting just a json res

相关标签:
2条回答
  • 2021-01-05 03:43

    I implement this and hope this is helpful all.

     var jsonResponse = 
      [{"Id":2,"Name":"Watch"},{"Id":3,"Name":"TV"},{"Id":4,"Name":""}]
    
     var items = JsonConvert.DeserializeObject<List<MyClass>>(jsonResponse);
    

    where MyClass is the entity

     public class MyClass
                {
                    public int Id { get; set; }
                    public string Name { get; set; }
                }
    
    0 讨论(0)
  • 2021-01-05 03:48

    You have an array of objects not strings. Create a class that maps the properties and deserialize into that,

    public class MyClass {
        public string id { get; set; }
        public string content { get; set; }
        public string ups { get; set; }
        public string downs { get; set; }
    }
    
    MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(download);
    

    There are only a few basic types in JSON, but it is helpful to learn and recognize them. Objects, Arrays, strings, etc. http://www.json.org/ and http://www.w3schools.com/json/default.asp are good resources to get started. For example a string array in JSON would look like,

    ["One", "Two", "Three"]
    
    0 讨论(0)
提交回复
热议问题