将json转换为c#对象列表
Json字符串:
{"movies":[{"id":"1","title":"Sherlock"},{"id":"2","title":"The Matrix"}]}
C#类:
public class Movie { public string title { get; set; }
}
C#将json转换为电影的c#列表:
JavaScriptSerializer jss = new JavaScriptSerializer();List<Movie> movies = jss.Deserialize<List<Movie>>(jsonString);
我的movies
变量最终是一个计数为0的空列表。我是否缺少某些内容?
回答:
您的C#类映射与json结构不匹配。
class MovieCollection { public IEnumerable<Movie> movies { get; set; }
}
class Movie {
public string title { get; set; }
}
class Program {
static void Main(string[] args)
{
string jsonString = @"{""movies"":[{""id"":""1"",""title"":""Sherlock""},{""id"":""2"",""title"":""The Matrix""}]}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
MovieCollection collection = serializer.Deserialize<MovieCollection>(jsonString);
}
}
以上是 将json转换为c#对象列表 的全部内容, 来源链接: utcz.com/qa/400081.html