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

I have the following code that works:

private string GetMovieCert(int ID)
        {
            try
            {
                var client = new RestClient("https://api.themoviedb.org/3/movie/" + ID.ToString() + "/release_dates?api_key=###");
                var request = new RestRequest(Method.GET);
                request.AddParameter("undefined", "{}", ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);

                List<char> items = response.Content.ToList<char>();               

                // Char version
                int hit = 0;
                string allres = "";
                foreach (var item in items)
                {
                    string f = item.ToString();
                    allres = allres + item.ToString();
                    if (f == "}")
                    {
                        if (allres.Contains("iso_3166_1\":\"US")) hit |= 3;
                        // if (allres.Contains("iso_3166_1")) hit |= 1;
                        // if (allres.Contains("US")) hit |= 2;
                        if (allres.Contains("\"certification\":\"\"")) hit |= 4;
                        if (hit == 3) break;
                        allres = "";
                        hit &= 3;
                    }
                    if (f == "]") hit = 0;
                }

                int pFrom = allres.IndexOf("certification") + "certification".Length;
                int pTo = allres.LastIndexOf("iso_639_1");

                String result = allres.Substring(pFrom, pTo - pFrom);
                return result.Split(':')[1].Replace(",", "").Replace("\"", ""); 
            }
            catch { return ""; }
        }

Is there a cleaner, more direct way to accomplish this? Using Linq or a JSON parser?

Thanks!

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

Jump to last post

Ok, I figured out a way to retrieve a movie's certification using a JSON query. I'm posting the code so that it may help someone.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DM.MovieApi;
using DM.MovieApi.ApiResponse;
using DM.MovieApi.ApiRequest;
using DM.MovieApi.MovieDb.Movies;
using DM.MovieApi.MovieDb.Genres;
using DM.MovieApi.MovieDb.Certifications;
using RestSharp;
using RestSharp.Deserializers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JsonQuery
{
    class Program
    {
        static void Main(string[] args)
        {
            string strCert = "";

            MovieDbFactory.RegisterSettings("###", "http://api.themoviedb.org/3/");

            var client = new RestClient("https://api.themoviedb.org/3/movie/<MOVIE_ID>/release_dates?api_key=<API_KEY>");
            var request = new RestRequest(Method.GET);
            request.AddParameter("undefined", "{}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            string json = response.Content;

            JObject rss = JObject.Parse(json);

            IEnumerable<JToken> certs = rss.SelectTokens("$.results[?(@.iso_3166_1 == 'US')].release_dates[?(@.certification)].certification");

            foreach (JToken item in certs) {strCert = item.ToString();}

            Console.WriteLine(strCert);
        }
    }
}

Hi @RichGodlewski Cool, thanks for the update. You'll find it always easier to use a real JSON parser as it will handle all of the proper serialization for you. Being able to work with native objects is always nicer 😃

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

Global

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

در صفحات مدیا

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

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

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

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

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

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

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

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

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

در صفحات بحث

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

تنظیمات

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

ورود