locked
Windows Store app - Parsing .JSON data from api call RRS feed

  • Question

  • I make this API call

    http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[APIKEY]&q=Toy+Story+3&page_limit=1

    The result is in .JSON  below

    {"total":7,"movies":[{"id":"770672122","title":"Toy Story 3","year":2010,"mpaa_rating":"G","runtime":103,"critics_consensus":"Deftly blending comedy, adventure, and honest emotion, Toy Story 3 is a rare second sequel that really works.","release_dates":{"theater":"2010-06-18","dvd":"2010-11-02"},"ratings":{"critics_rating":"Certified Fresh","critics_score":99,"audience_rating":"Upright","audience_score":91},"synopsis":"Pixar returns to their first success with Toy Story 3. The movie begins with Andy leaving for college and donating his beloved toys -- including Woody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crew meets new friends, including Ken (Michael Keaton), they soon grow to hate their new surroundings and plan an escape. The film was directed by Lee Unkrich from a script co-authored by Little Miss Sunshine scribe Michael Arndt. ~ Perry Seibert, Rovi","posters":{"thumbnail":"http://content6.flixster.com/movie/11/13/43/11134356_mob.jpg","profile":"http://content6.flixster.com/movie/11/13/43/11134356_pro.jpg","detailed":"http://content6.flixster.com/movie/11/13/43/11134356_det.jpg","original":"http://content6.flixster.com/movie/11/13/43/11134356_ori.jpg"},"abridged_cast":[{"name":"Tom Hanks","id":"162655641","characters":["Woody"]},{"name":"Tim Allen","id":"162655909","characters":["Buzz Lightyear"]},{"name":"Joan Cusack","id":"162655020","characters":["Jessie the Cowgirl"]},{"name":"Ned Beatty","id":"162672460","characters":["Lots-o'-Huggin' Bear","Lotso"]},{"name":"Don Rickles","id":"341817905","characters":["Mr. Potato Head"]}],"alternate_ids":{"imdb":"0435761"},"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json","alternate":"http://www.rottentomatoes.com/m/toy_story_3/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json","clips":"http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/clips.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/similar.json"}},{"id":"9414","title":"Toy Story 2","year":1999,"mpaa_rating":"G","runtime":92,"critics_consensus":"Toy Story 2 employs inventive storytelling, gorgeous animation, and a top notch voice cast to deliver another rich moviegoing experience for all ages, one that's arguably even better than its predecessor.","release_dates":{"theater":"1999-11-24","dvd":"2000-10-17"},"ratings":{"critics_rating":"Certified Fresh","critics_score":100,"audience_rating":"Upright","audience_score":72},"synopsis":"","posters":{"thumbnail":"http://content6.flixster.com/movie/10/93/63/10936392_mob.jpg","profile":"http://content6.flixster.com/movie/10/93/63/10936392_pro.jpg","detailed":"http://content6.flixster.com/movie/10/93/63/10936392_det.jpg","original":"http://content6.flixster.com/movie/10/93/63/10936392_ori.jpg"},"abridged_cast":[{"name":"Tom Hanks","id":"162655641","characters":["Woody"]},{"name":"Tim Allen","id":"162655909","characters":["Buzz Lightyear"]},{"name":"Joan Cusack","id":"162655020","characters":["Jessie the Cowgirl"]},{"name":"Kelsey Grammer","id":"162660300","characters":["Stinky Pete the Prospector"]},{"name":"Don Rickles","id":"341817905","characters":["Mr. Potato Head"]}],"alternate_ids":{"imdb":"0120363"},"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/9414.json","alternate":"http://www.rottentomatoes.com/m/toy_story_2/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/9414/cast.json","clips":"http://api.rottentomatoes.com/api/public/v1.0/movies/9414/clips.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/9414/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/9414/similar.json"}}],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Toy+Story&page_limit=2&page=1","next":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Toy+Story&page_limit=2&page=2"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"}

    How do i parse this using the following class?

    public class Movies
    {
    
    public String title = {get;set;}
    
    
    public String thumbnail  = {get;set;}
    
    
    
    }

    I want to end up with a list of the Movies class.  Filled with all Movies object found in the response

    Monday, June 24, 2013 7:22 PM

Answers

  • You know NUget?

    Add Json.Net to your project with nu get then use this sample:

     
    public async void DonwloadJson()
            {
                HttpClient client = new HttpClient();
                var stream = await client.GetStreamAsync(""); // fill in the right url
                var streamReader = new StreamReader(stream);
                var reader = new JsonTextReader(streamReader);
    
                JObject json = JObject.Load(reader);
                JsonSerializer serializer = new JsonSerializer();
                var result = serializer.Deserialize<Movies>(json.CreateReader());
            }
    use this website: http://json2csharp.com/ to generate the c# class that can help to deserialize the json

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by kalel111 Tuesday, June 25, 2013 12:58 AM
    Monday, June 24, 2013 9:37 PM
  • this are all the namespaces i have in that class:

    using Newtonsoft.Json;
        using Newtonsoft.Json.Linq;
        using System;
        using System.Collections.Generic;
        using System.Composition;
        using System.Globalization;
        using System.IO;
        using System.Linq;
        using System.Net;
        using System.Net.Http;
        using System.Text;
        using System.Threading.Tasks;

    and you know you can do right click on it and then let Visual Studio resolve the namespace.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by kalel111 Tuesday, June 25, 2013 12:58 AM
    Monday, June 24, 2013 11:58 PM

All replies

  • Json.Net library from nuget

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Proposed as answer by Sachin S Monday, June 24, 2013 8:54 PM
    • Unproposed as answer by kalel111 Monday, June 24, 2013 9:36 PM
    Monday, June 24, 2013 7:30 PM
  • What? Can you explain your answer... 
    Monday, June 24, 2013 8:08 PM
  • You know NUget?

    Add Json.Net to your project with nu get then use this sample:

     
    public async void DonwloadJson()
            {
                HttpClient client = new HttpClient();
                var stream = await client.GetStreamAsync(""); // fill in the right url
                var streamReader = new StreamReader(stream);
                var reader = new JsonTextReader(streamReader);
    
                JObject json = JObject.Load(reader);
                JsonSerializer serializer = new JsonSerializer();
                var result = serializer.Deserialize<Movies>(json.CreateReader());
            }
    use this website: http://json2csharp.com/ to generate the c# class that can help to deserialize the json

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by kalel111 Tuesday, June 25, 2013 12:58 AM
    Monday, June 24, 2013 9:37 PM
  • It seems i dont have the proper namespace in my class.

    Heres a screenshot of what i get after using your code. AND yes i did get the NUget package and installed.

    I tried looking for  a namespace online, but i didnt find any..

    Monday, June 24, 2013 10:01 PM
  • this are all the namespaces i have in that class:

    using Newtonsoft.Json;
        using Newtonsoft.Json.Linq;
        using System;
        using System.Collections.Generic;
        using System.Composition;
        using System.Globalization;
        using System.IO;
        using System.Linq;
        using System.Net;
        using System.Net.Http;
        using System.Text;
        using System.Threading.Tasks;

    and you know you can do right click on it and then let Visual Studio resolve the namespace.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by kalel111 Tuesday, June 25, 2013 12:58 AM
    Monday, June 24, 2013 11:58 PM