RestSharp deserialize JSON content(represent an object contains an byte array) error

前端 未结 3 621
夕颜
夕颜 2021-02-18 19:24

The Client side receives a formal JSON content \"{\\\"Id\\\":[1,2,3],\\\"Size\\\":56}\", but get an error in deserialization the byte array.

1 Error occurs

相关标签:
3条回答
  • 2021-02-18 20:03

    I have run into this issue, too. My solution was to use RestSharp to perform a raw execute and use Json.NET to deserialize the result:

    var response = client.Execute(request);
    var keyResponse = JsonConvert.DeserializeObject<key>(response.Content);
    

    keyResponse should now be an instance of your key class deserialized from the JSON content.

    0 讨论(0)
  • 2021-02-18 20:27

    JsonDeserializer from RestSharp can not deserialize array. Instead of byte[] use List<byte>. For more information see https://github.com/restsharp/RestSharp/wiki/Deserialization

    0 讨论(0)
  • 2021-02-18 20:28

    In addition to Chris Hogan's reply, I'd like to point out that I got this error when RestSharp incorrectly used the default serializer instead of the custom JSON.NET serializer I had assigned.

    The reason for this was that I added a handler with content type application/json whereas the API I was getting the response from returned the content as text/json.

    So by changing the AddHandler call to AddHandler("text/json", jsonDeserializer), I resolved the issue.

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