RestSharp Deserialization with JSON Array

后端 未结 5 1140
北海茫月
北海茫月 2021-01-01 15:27

I have a JSON response that I\'m trying to deserialize with RestSharp, and it looks like this:

{\"devices\":[{\"device\":{\"id\":7,\"deviceid\":\"abc123\",\"         


        
相关标签:
5条回答
  • 2021-01-01 15:29

    RestShartp doesn't support DataAnnotation/DataMember, rename your properties with no maj:

    • Devices -> devices
    • Device -> device

    AND don't forget the {get; set;} ;).

    0 讨论(0)
  • 2021-01-01 15:31

    RestSharp only operates on properties, it does not deserialize to fields, so make sure to convert your Devices and Device fields to properties.

    Also, double check the Content-Type of the response, if the responses is something non-default, RestSharp may not uses the JsonDeserializer at all. See my answer on RestSharp client returns all properties as null when deserializing JSON response

    0 讨论(0)
  • 2021-01-01 15:37

    I had a slightly different issue when my deserialization POCO contained an array..

    Changing it from Devices[] to List<Devices> resolved the issue and it deserialized correctly.

    0 讨论(0)
  • 2021-01-01 15:52

    Something that I ran into is, it does not work if your using interfaces like: IEnumerable or IList, it has to be a concrete type.

    This will not work, where as it does for some other json serializers like json.net.

    public class DevicesList
    {
        public IEnumerable<DeviceContainer> Devices { get; set; }
    }
    
    public class DeviceContainer
    {
       ...
    }
    

    it would have to be something like this:

    public class DevicesList
    {
        public List<DeviceContainer> Devices { get; set; }
    }
    
    public class DeviceContainer
    {
       ...
    }
    
    0 讨论(0)
  • 2021-01-01 15:54

    My problem was entirely different, I naively thought JsonDeserializer supports JsonProperty attribute, but thats not true. So when trying to deserialize into

    public class AvailableUserDatasApi
    {
        [JsonProperty("available-user-data")]
        public List<AvailableUserDataApi> AvailableUserDatas { get; set; }
    }
    

    it failed.. But changing AvailableUserDatas to AvailableUserData was enough for things to start working.

    0 讨论(0)
提交回复
热议问题