C# data contract for complex JSON object

后端 未结 3 1641
暗喜
暗喜 2021-01-21 11:32

This is probably something really simple and I looked everywhere and tried everything I could come up with. So I apologize if this is a simple search and I was just looking for

3条回答
  •  Happy的楠姐
    2021-01-21 11:55

    Another option is to use the dynamic keyword. You could use a list of this type for data (per below).

    [DataContract]
    public class Update_DB
    {
        [DataMember(Name = "appname", IsRequired = true)]
        public string appname { get; set; }
        [DataMember]
        public string key { get; set; }
    
        [DataMember(Name = "data", IsRequired = true)]
        public List data { get; set; }
    
        [DataMember]
        public string updateId { get; set; }
        [DataMember]
        public string updateTS { get; set; }
        [DataMember]
        public string creationUser { get; set; }
    }
    

    From there, you could use the object by deserializing with JSON.Net, and access into the dynamic data object (assuming you know something about the shape of this dynamic object). Something like below will work based on the input string from the original post.

    Update_DB dataObj = JsonConvert.DeserializeObject(objectJSON);
    string test = dataObj.data[1].connectorType; //evaluates to "webserver-to-appserver"
    

提交回复
热议问题