这种东西先用现成的转换工具把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方式保存。
"rating":{"max":10,"average":7.3,"stars":"40","min":0}
这个按照网页上说明是dict类型。JArray昨天测试过,不成功。
测试1
avatars = jo["avatars"][0]["name"].ToString();
失败
测试2
JToken a = jo["avatars"];
string[] b = a.ToArray<>;——这个好像是LINQ语法
失败3.具体就是我不知道用什么类型去对应dict。C#的dictorary用过一次,不熟。array也不知道怎么弄。
我自己尝试抓取了imdb的数据,imdbapi提供的json很容易,我直接用C#自带的DataContractJsonSerializer就搞定了,但是豆瓣的要复杂很多,我先尝试写了douban类,发现完全不知道怎么弄对应的类型。
{
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方式保存。
杨子姗name=jo.casts.avatars[0].name //这样可以吗
jo["casts"][0]["avatars"]["name"].ToString();
杨子姗name=jo.casts[0].avatars.name
public class douban
{
[DataMember(Order = 0)]
public rating rating { get; set; } [DataMember(Order = 1)]
public string reviews_count { get; set; } [DataMember(Order = 2)]
public string wish_count { get; set; } [DataMember(Order = 3)]
public string collect_count { get; set; } [DataMember(Order = 4)]
public string douban_site { get; set; } [DataMember(Order = 5)]
public string year { get; set; } [DataMember(Order = 6)]
public images images { get; set; } [DataMember(Order = 7)]
public string alt { get; set; } [DataMember(Order = 8)]
public string id { get; set; } [DataMember(Order = 9)]
public string mobile_url { get; set; } [DataMember(Order = 10)]
public string title { get; set; } [DataMember(Order = 11)]
public string do_count { get; set; } [DataMember(Order = 12)]
public string seasons_count { get; set; } [DataMember(Order = 13)]
public string schedule_url { get; set; } [DataMember(Order = 14)]
public string episodes_count { get; set; } [DataMember(Order = 15)]
public string[] genres { get; set; } [DataMember(Order = 16)]
public string[] countries { get; set; } [DataMember(Order = 17)]
public List<people_class> casts { get; set; } [DataMember(Order = 18)]
public string current_season { get; set; } [DataMember(Order = 19)]
public string original_title { get; set; } [DataMember(Order = 20)]
public string summary { get; set; } [DataMember(Order = 21)]
public string subtype { get; set; } [DataMember(Order = 22)]
public List<people_class> directors { get; set; } [DataMember(Order = 23)]
public string comments_count { get; set; } [DataMember(Order = 24)]
public string ratings_count { get; set; } [DataMember(Order = 25)]
public string[] aka { get; set; } } public struct rating
{
public string max { get; set; }
public string average { get; set; }
public string stars { get; set; }
public string min { get; set; }
}; public struct images
{
public string small { get; set; }
public string large { get; set; }
public string medium { get; set; }
}; public class people_class
{
public List<images> avatars { get; set; }
public string alt { get; set; }
public string id { get; set; }
public string name { get; set; }
}
直接拿来序列化
douban db = JSON.parse<douban>(srcString);