دعم الموقع

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)

لم تجد الفلم أو المسلسل ؟ سجل دخولك و انشئها

عام

s ركز شريط البحث
p افتح قائمة الملف الشخصي
esc اغلق النافذة المفتوحة
? افتح نافذة اختصارات لوحة المفاتيح

على كافة صفحات الوسائط

b ارجع للخلف (او للصفحة الام عند التطبيق)
e انتقل لصفحة التعديل

على كافة صفحات موسم المسلسل

(السهم الايمن) انتقل للموسم التالي
(السهم الايسر) انتقل للموسم السابق

على كافة صفحات حلقة المسلسل

(السهم الايمن) انتقل للحلقة التالية
(السهم الايسر) انتقل للحلقة السابقة

على كافة صفحات الصور

a افتح صفحة اضافة الصورة

على كافة صفحات التعديل

t افتح محدد الترجمة
ctrl+ s ارسال النموذج

على صفحات المناقشة

n انشى نقاش جديد
w تبديل حالة المتابعة
p تبديل عام / خاص
c تبديل اغلاق / فتح
a افتح الانشطة
r رد على النقاش
l انتقل لأخر رد
ctrl+ enter أرسل رسالتك
(السهم الايمن) الصفحة التالية
(السهم الايسر) الصفحة السابقة

الاعدادات

هل تريد تقييم او اضافة هذا العنصر للقائمة؟

تسجيل الدخول