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);
我的电影变量结束是一个空列表,count = 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); } }