比如读取
http://api.douban.com/v2/movie/subject/25870084
可以获得
{"rating":{"max":10,"average":7.3,"stars":"40","min":0},"reviews_count":659,"wish_count":11153,"collect_count":27792,"douban_site":"http:\/\/site.douban.com\/MissGranny\/","year":"2015","images":{"small":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/ipst\/public\/p2219440250.jpg","large":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/lpst\/public\/p2219440250.jpg","medium":"http:\/\/img3.douban.com\/view\/movie_poster_cover\/spst\/public\/p2219440250.jpg"},"alt":"http:\/\/movie.douban.com\/subject\/25870084\/","id":"25870084","mobile_url":"http:\/\/movie.douban.com\/subject\/25870084\/mobile","title":"重返20岁","do_count":null,"seasons_count":null,"schedule_url":"http:\/\/movie.douban.com\/subject\/25870084\/cinema\/","episodes_count":null,"genres":["喜剧","爱情","奇幻"],"countries":["中国大陆","韩国","台湾","香港"],"casts":[{"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/1360212531.4.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/1360212531.4.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/1360212531.4.jpg"},"alt":"http:\/\/movie.douban.com\/celebrity\/1312862\/","id":"1312862","name":"杨子姗"},{"avatars":{"small":"http:\/\/img5.douban.com\/img\/celebrity\/small\/44939.jpg","large":"http:\/\/img5.douban.com\/img\/celebrity\/large\/44939.jpg","medium":"http:\/\/img5.douban.com\/img\/celebrity\/medium\/44939.jpg"},"alt":"http:\/\/movie.douban.com\/celebrity\/1058399\/","id":"1058399","name":"归亚蕾"},{"avatars":{"small":"http:\/\/img3.douban.com\/img\/celebrity\/small\/22033.jpg","large":"http:\/\/img3.douban.com\/img\/celebrity\/large\/22033.jpg","medium":"http:\/\/img3.douban.com\/img\/celebrity\/medium\/22033.jpg"},"alt":"http:\/\/movie.douban.com\/celebrity\/1211987\/","id":"1211987","name":"陈柏霖"},{"avatars":{"small":"http:\/\/img5.douban.com\/img\/celebrity\/small\/1388465430.97.jpg","large":"http:\/\/img5.douban.com\/img\/celebrity\/large\/1388465430.97.jpg","medium":"http:\/\/img5.douban.com\/img\/celebrity\/medium\/1388465430.97.jpg"},"alt":"http:\/\/movie.douban.com\/celebrity\/1337001\/","id":"1337001","name":"鹿晗"}],"current_season":null,"original_title":"重返20岁","summary":"坏脾气的老奶奶(归亚蕾 饰),除了前管家(王德顺 饰)之外,没有人喜欢她,连媳妇也因与她同住一个屋檐下压力过大而住院,家人们毅然决然地决定送奶奶去住养老院。奶奶伤心之际路过照相馆,想留下最后的身影,没想到这一拍竟将老奶奶的外貌变成了20岁时的年轻模样(杨子姗 饰),随之而来的奇怪际遇不但让她加入了乐团当主唱,更引发音乐总监(陈柏霖 饰)与乐团团员(鹿晗 饰)的争锋吃醋,她再一次有了“恋爱的感觉”;而就在她开始尝到“再年轻一次”的甜头时,却因一场意外而面临了重大抉择,究竟她该留在20岁的外貌继续享受人生,还是回到70岁的老奶奶面对迟暮生活?","subtype":"movie","directors":[{"avatars":{"small":"http:\/\/img5.douban.com\/img\/celebrity\/small\/1369639357.39.jpg","large":"http:\/\/img5.douban.com\/img\/celebrity\/large\/1369639357.39.jpg","medium":"http:\/\/img5.douban.com\/img\/celebrity\/medium\/1369639357.39.jpg"},"alt":"http:\/\/movie.douban.com\/celebrity\/1189801\/","id":"1189801","name":"陈正道"}],"comments_count":16131,"ratings_count":25898,"aka":["重返二十岁","Miss Granny","20?? ?? ? ?"]}
我试用了C#自带的DataContractJsonSerializer和Newtonsoft.Json,感觉Newtonsoft.Json好用一些。
海报比较容易获取,大海报是
JObject jo = (JObject)JsonConvert.DeserializeObject(srcString);
string large = jo["images"]["large"].ToString();
详细的api介绍可见http://developers.douban.com/wiki/?title=movie_v2
只会C#,JAVA啥的都没有学过,第一次接触JSON,对于页面上所谓的dict和array数据类型均不熟悉,不知道如何用C#处理
casts字段为array,rating字段为dict,如何能用比较规则的写法,变成对应的C#类型??
------解决思路----------------------
这种东西先用现成的转换工具把json转c#的类,比如:http://jsonutils.com/。当然它不是那么智能,自己再调整下就好。弄好以后如下:
public class Rating
{
public int Min { get; set; }
public int Max { get; set; }
public double Average { get; set; }
public string Stars { get; set; }
}
public class Celebrity
{
public string Id { get; set; }
public string Name { get; set; }
public string Alt { get; set; }
public Images Avatars { get; set; }
}
public class Images
{
public string Small { get; set; }
public string Large { get; set; }
public string Medium { get; set; }
}
public class Movie
{
public string Id { get; set; }
public string Title { get; set; }
[JsonProperty("original_title")]
public string OriginalTitle { get; set; }
public IList<string> Aka { get; set; }
public string Alt { get; set; }
[JsonProperty("mobile_url")]
public string MobileUrl { get; set; }
public Rating Rating { get; set; }
[JsonProperty("ratings_count")]
public int RatingsCount { get; set; }
[JsonProperty("wish_count")]
public int WishCount { get; set; }
[JsonProperty("collect_count")]
public int CollectCount { get; set; }
[JsonProperty("do_count")]
public int? DoCount { get; set; }
public Images Images { get; set; }
public string Subtype { get; set; }
public IList<Celebrity> Directors { get; set; }
public IList<Celebrity> Casts { get; set; }
[JsonProperty("douban_site")]
public string DoubanSite { get; set; }
public string Year { get; set; }
public IList<string> Genres { get; set; }
public IList<string> Countries { get; set; }
public string Summary { get; set; }
[JsonProperty("comments_count")]
public int CommentsCount { get; set; }
[JsonProperty("reviews_count")]
public int ReviewsCount { get; set; }
[JsonProperty("seasons_count")]
public int? SeasonsCount { get; set; }
[JsonProperty("current_season")]
public int? CurrentSeason { get; set; }
[JsonProperty("episodes_count")]
public int? EpisodesCount { get; set; }
[JsonProperty("schedule_url")]
public string ScheduleUrl { get; set; }
}
// 使用的时候
var movie = JsonConvert.DeserializeObject<Movie>(jsonText);
PS:还需要注意,在你给的json里面,最后的韩文变成了问号,估计这是用ansi方式保存了原本是utf8的文字导致的,应该以utf8方式保存。