[英]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.