پشتیبانی پایگاه داده‌ فیلم

I am quite familiar with C# but new to JSON. I cannot figure out how to parse the data returned by the API.

First I tried this...

        string url = "https://api.themoviedb.org/3/movie/tt1872181?api_key=" + API_KEY;

        var request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "GET";
        request.Accept = "application/json";
        request.ContentLength = 0;

        string responseContent = null;

        WebResponse response = request.GetResponse();
        //StreamReader reader = new StreamReader(response.GetResponseStream());

        StreamReader stream = new StreamReader(url);
        string text = stream.ReadToEnd();

And I DID receive the data from the API in JSON format. I could parse this data the 'old fashion' way but I know that isn't the correct way to do it nor is it efficient. So, in an effort to parse the JSON data the correct way, I changed my script to this...

        // Creates an HttpWebRequest with the specified URL. 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        // Send the request and wait for response.          
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Get the response stream
        Stream responseStream = response.GetResponseStream();
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(movie));

        object objResponse = (movie)jsonSerializer.ReadObject(responseStream);

        movie jsonResponse = objResponse as movie;

        response.Close();

Which seems to work. The error I am getting, I believe, has to do with the MOVIE class. The error I am getting is this.

An unhandled exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll

Additional information: Type 'emma.movie' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

And, finally, here is my movie class:

class movie
{
    public int Id { get; set; }
    public string ImdbId { get; set; }
    public string Title { get; set; }
    public string OriginalTitle { get; set; }
    public string Status { get; set; }
    public string Tagline { get; set; }
    public string Overview { get; set; }
    public string Homepage { get; set; }

    public string BackdropPath { get; set; }
    public string PosterPath { get; set; }

    public bool Adult { get; set; }

    public List<string> BelongsToCollection { get; set; }
    public List<string> Genres { get; set; }

    public DateTime ReleaseDate { get; set; }
    public long Revenue { get; set; }
    public long Budget { get; set; }
    public int Runtime { get; set; }

    public double Popularity { get; set; }
    public double VoteAverage { get; set; }
    public int VoteCount { get; set; }

    public List<string> ProductionCompanies { get; set; }
    public List<string> ProductionCountries { get; set; }
    public List<string> SpokenLanguages { get; set; }

    public string AlternativeTitles { get; set; }
    public string Releases { get; set; }
    public string Casts { get; set; }
    public string Images { get; set; }
    public string Keywords { get; set; }
    public string Trailers { get; set; }
    public string Translations { get; set; }
    public string SimilarMovies { get; set; }
    public string SearchContainer { get; set; }
    public List<string> Changes { get; set; }

}

Any help you can provide is greatly appreciated.

Greg

5 پاسخ (در صفحه 1 از 1)

Jump to last post

Hi gbgordy7@yahoo.com,

I can't offer much help with C# as I have never even opened a C# file but I am wondering, have you looked at how the fields are assigned in one of the C# libraries like this one? It looks like a field serialization issue but without knowing which field, I am not sure what it could be.

Yeah. I have looked through all of the libraries and cannot find anything to help me with my specific problem. Thank you for your input tho...I will keep searching.

That did it! Thank you so much!

I spoke too soon. This will return individual movies like when I pull up something by the IMDB ID or by the TMDB ID. But, if I do a search, it returns all NULL values. I imagine I need to return the results as a collection of movies or something similar. Can someone please point me in the right direction with this?

Thank you in advance.

Greg

You are probabyl right about the wrong collection type. You could try dynamics if you're using a newer c# version. So you can easily debug what result you get. Also check this http://james.newtonking.com/json (or better get it from nuget)

فیلم و نمایش تلویزیونی را نمی‌توانید پیدا کنید؟ به سیستم وارد شوید تا آن را ایجاد کنید.

Global

s تمرکز بر منوی جستجو
p منوی پروفایل باز شود
esc بستن پنجره باز
? پنجره میانبرهای صفحه‌کلید باز شود

در صفحات مدیا

b بازگشت به عقب (یا در صورت لزوم به منشا)
e برو به صفحه ویرایش

در صفحات فصل تلویزیونی

(فلش سمت راست) برو به فصل بعد
(پیکان سمت چپ) برو به نشست قبلی

در صفحات قسمت تلویزیونی

(فلش سمت راست) برو به قسمت بعد
(پیکان سمت چپ) برو به قسمت قبلی

در تمام صفحات تصویر

a پنجره افزودن تصویر باز شود

در تمام صفحات ویرایش

t انتخابگر ترجمه باز شود
ctrl+ s ثبت از

در صفحات بحث

n ایجاد بحث جدید
w تغییر وضعیت وضعیت تماشا
p تغییر وضعیت عمومی/خصوصی
c تغییر وضعیت بسته/باز
a گشایش صفحه فعالیت
r پاسخ به بحث
l برو به آخرین پاسخ
ctrl+ enter پیام خود را ثبت کنید
(فلش سمت راست) صفحه بعد
(پیکان سمت چپ) صفحه قبلی

تنظیمات

آیا می‌خواهید به این مورد امتیاز دهید یا به فهرست اضافه کنید؟

ورود