简体   繁体   English

反序列化对象时,无法将类型为System.String的对象转换为类型为System.Byte []

[英]Cannot convert object of type System.String to type System.Byte[] when deserializing object

I have a class in C# that maps InspectionDetails sent from a mobile app (a JSON object) to a Web API service. 我在C#中有一个类,它将从移动应用程序(JSON对象)发送的InspectionDetails映射到Web API服务。 I recently added a property to the class to handle sending images from the mobile app. 我最近向该类添加了一个属性,以处理从移动应用发送图像。 But when deserializing the JSON I get the error 但是在反序列化JSON时出现错误

Cannot convert object of type System.String to type System.Byte[] 无法将类型为System.String的对象转换为类型为System.Byte []

My class in C# looks like this. 我在C#中的课程如下所示。

public class InspectionDetails
{
    public int UserId { get; set; }
    public List<byte[]> Images { get; set; }
}

I then attempt to deserialize the JSON as follows. 然后,我尝试如下反序列化JSON。

result = new JavaScriptSerializer().Deserialize<T>(jsonObject); // throws error

An example of the JSON is as follows: JSON的示例如下:

{"UserId":1001872,"Images":["fFCVKyfUYq72+N0M3IzaihLH0/rMDSwdbPHTXpwkQTw+Fp1NlYKvyyp0e+yIxoOOVe24Ous2ESsAfD4kIeN=="]}

How do I deserialize a byte array in JSON? 如何在JSON中反序列化字节数组? I need to deserializethe JSON to get to the images. 我需要反序列化JSON才能到达图像。

This seems to work with JSON.NET 8.0.2 这似乎适用于JSON.NET 8.0.2

    var jsonSettings = new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.Objects,
        TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
    };
    var result = JsonConvert.DeserializeObject<T>(jsonObject, jsonSettings);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误 - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) 无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.String[]' Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 无法将“System.Byte”类型的 object 转换为“System.String”类型。 - Unable to cast object of type 'System.Byte' to type 'System.String'.' 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 无法将“ System.Object []”转换为类型“ System.String []” - Cannot convert 'System.Object[]' to the type 'System.String[]' 尝试解析JSON响应时出现错误-无法将类型为&#39;System.String&#39;的对象转换为类型&#39;&#39; - Getting error when trying to parse JSON Response - Cannot convert object of type 'System.String' to type ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM