1

我有一个包含此 JSON 数据的 URL:

[{"title":"Snow White & the Huntsman","year":2012,"released":1338534000,"url":"http://localhost/movie/snow-white-and-the-huntsman-2012","trailer":"http://youtube.com/watch?v=11Wn-_uyT48","runtime":127,"tagline":"","overview":"After the Evil Queen marries the King, she performs a violent coup in which the King is murdered and his daughter, Snow White, is taken captive. Almost a decade later, a grown Snow White is still in the clutches of the Queen. In order to obtain immortality, The Evil Queen needs the heart of Snow White. After Snow escapes the castle, the Queen sends the Huntsman to find her in the Dark Forest.","certification":"PG-13","imdb_id":"tt1735898","tmdb_id":"58595","poster":"http://trakt.us/images/posters_movies/180748.1.jpg","images":{"poster":"http://trakt.us/images/posters_movies/180748.1.jpg","fanart":"http://trakt.us/images/fanart_movies/180748.1.jpg"},"watchers":15,"ratings":{"percentage":68,"votes":105,"loved":71,"hated":34},"genres":["Adventure","Fantasy","Action","Drama"]}

我正在尝试解析它并将其显示在我的地铁应用程序中,但我无法让它为我的生活工作!我已经尝试了很多这样的事情:

        string trendingURL = "http://Localhost/movies/trending.json/";


        MovieDetails newMovie = new MovieDetails();
        newMovie.title = "";
        newMovie.cover = "";

        //DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MovieDetails));

        //StreamReader sr = new Stream(trendingURL);

        HttpClient client = new HttpClient();

        HttpResponseMessage response = await client.GetAsync(trendingURL);
        var str = await response.Content.ReadAsStreamAsync();
        var ser = new DataContractJsonSerializer(typeof(MovieDetails));
        var collection = (MovieDetails)ser.ReadObject(str);
        var results = collection.title;
4

3 回答 3

0

您尝试过Json.NET库吗?根据 Codeplex,它也支持 Windows 8,所以你可以这样做:

var o = JObject.Parse(YOUR_JSON_STRING);
var newMovie = new MovieDetails();
newMovie.title = (string)o["title"];
newMovie.cover = (string)o["cover"];
于 2012-08-31T19:50:36.597 回答
0

使用Json.Net

using (var wc = new WebClient())
{
    string json = await wc.DownloadStringTaskAsync(trendingURL);
    dynamic obj = JsonConvert.DeserializeObject(json);
    foreach (var item in obj)
    {
        Console.WriteLine("{0} - {1} - {2} - {3}", 
                    item.title, 
                    item.year, 
                    item.images.poster, 
                    item.ratings.votes);
    }
}
于 2012-09-01T00:29:37.970 回答
0

使用speakeasy:

var client = new HttpClient("http://Localhost/");
var details = client.Get("movies/trending.json").OnOk().As<MovieDetails>();

你可以使用nuget安装speakeasy。

于 2012-09-01T05:09:14.380 回答