Deserialize nested JSON into C# objects

后端 未结 3 1912
野的像风
野的像风 2020-11-27 06:55

I am getting JSON back from an API that looks like this:

{
  \"Items\": {
    \"Item322A\": [{
      \"prop1\": \"string\",
      \"prop2\": \"string\",
             


        
3条回答
  •  误落风尘
    2020-11-27 07:09

    Use this this site for representation:

    https://quicktype.io/csharp/

    something like this may help you

    public class Item322A
    {
        public string prop1 { get; set; }
        public string prop2 { get; set; }
        public int prop3 { get; set; }
        public bool prop4 { get; set; }
    }
    
    public class Item2B
    {
        public string prop1 { get; set; }
        public string prop2 { get; set; }
        public int prop3 { get; set; }
        public bool prop4 { get; set; }
    }
    
    public class Items
    {
        public List Item322A { get; set; }
        public List Item2B { get; set; }
    }
    
    public class jsonObject
    {
        public Items Items { get; set; }
        public List Errors { get; set; }
    }
    

    Here is how to deserialize (use JsonConvert class):

    jsonObject ourlisting = JsonConvert.DeserializeObject(strJSON);
    

提交回复
热议问题