具有字符串列表的JSON DeserializeObject [英] JSON DeserializeObject that has a list of strings
问题描述
[
{
"Title": "TOY STORY 4",
"Genre": "COMEDY",
"Actors": [
"Tom Hanks",
"Tim Allen",
"Annie Potts"
],
"Id": 1
},
{
"Title": "The Matrix",
"Genre": "Action",
"Actors": [
"Keanu Reeves",
"Laurence Fishburne",
"Carrie-Anne Moss"
],
"Id": 2
}
]
我有一个要转换C#对象的JSON字符串:
I have this JSON string that I want to convert C# object :
public class Movies1
{
public string Title { get; set; }
public string Genre{ get; set; }
public List<Actor> Actors { get; set; }
public int id { get; set; }
}
public class Actor
{
public string Actors { get; set; }
public int Id { get; set; }
}
我的反序列化代码:
List<movies> = JsonConvert.DeserializeObject<List<Movies1>>(json);
我遇到以下错误:
"ExceptionMessage":无法将System.String强制转换或转换为 XXXXX.Models.Actor.", "ExceptionType":"System.ArgumentException", "StackTrace":在Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object 值类型 initialType,类型为targetType)\ r \ n,位于Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue,CultureInfo文化,在键入Type targetType)\ r \ n Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader 读者,对象 值,CultureInfo文化,JsonContract合同,Type targetType)"
"ExceptionMessage": "Could not cast or convert from System.String to XXXXX.Models.Actor.", "ExceptionType": "System.ArgumentException", "StackTrace": " at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)\r\n at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)"
推荐答案
似乎Actors
是一个字符串列表,您需要的是这样的东西:
It seems the Actors
is a list of strings, what you need is something like this:
public class Movies1
{
public string Title { get; set; }
public string GENRE { get; set; }
public List<string> Actors { get; set; }
public int Id { get; set; }
}
public class Root
{
public List<Movies1> Movies { get; set; }
}
然后:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
这篇关于具有字符串列表的JSON DeserializeObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!