Ejemplo n.º 1
0
        static void Main()
        {
            using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
            {
                if (reader.SkipTo("seasons", 7))
                {
                    // move past the field name "name"
                    if (JsonNodeType.Key != reader.NodeType || reader.Read())
                    {
                        Console.WriteLine(reader.ParseSubtree());
                    }
                }
            }
            // or...
            using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
            {
                if (reader.SkipToField("created_by"))
                {
                    if (reader.SkipToIndex(0))
                    {
                        reader.Read();
                        if (reader.SkipToField("name"))
                        {
                            if (reader.Read())
                            {
                                Console.WriteLine(reader.Value);
                            }
                        }
                    }
                }
            }

            return;

            Tmdb.ApiKey     = ApiKey;
            Tmdb.CacheLevel = JsonRpcCacheLevel.Aggressive;

            // hit it *hard* - 10 pages of movies, 10 of TV
            foreach (var movie in TmdbMovie.GetTopRated(0, 9))
            {
                _RunMovieDemo(movie.Id);
            }

            foreach (var show in TmdbShow.GetTopRated(0, 9))
            {
                _RunShowDemo(show.Id);
            }
        }
Ejemplo n.º 2
0
 static void _Example1()
 {
     using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
     {
         while (reader.Read())
         {
             Console.Write(reader.NodeType);
             // only keys and values have a Value
             if (JsonNodeType.Value == reader.NodeType ||
                 JsonNodeType.Key == reader.NodeType)
             {
                 Console.Write(" " + reader.Value);
             }
             Console.WriteLine();
         }
     }
 }
Ejemplo n.º 3
0
 static void Main(string[] args)
 {
     // very long. Uncomment if you want it
     //_Example1();
     _Example2();
     _Example3();
     _Example4();
     _Example5();
     _Example6();
     _Example7();
     // bonus code (not in article)
     using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
     {
         if (reader.SkipTo("seasons", 7, "episodes", 3, "guest_stars", 0, "character"))
         {
             // move past the field name if it's a key.
             if (JsonNodeType.Key != reader.NodeType || reader.Read())
             {
                 Console.WriteLine(reader.ParseSubtree());
             }
         }
     }
 }
Ejemplo n.º 4
0
        static void Main()
        {
            var url = "http://api.themoviedb.org/3/tv/2129?api_key=c83a68923b7fe1d18733e8776bba59bb";

            using (var reader = JsonTextReader.CreateFromUrl(url))
            {
                // skip to "$.created_by[1].name" <-- JSON path syntax
                if (reader.SkipTo("created_by", 0, "name"))
                {
                    // we're currently on the *key*/field name
                    // we have to move to the value if we want 
                    // just that.
                    if (reader.Read())
                    {
                        Console.WriteLine(reader.ParseSubtree());
                    }
                    else                     // below should never execute
                    {
                        Console.WriteLine("Sanity check failed, key has no value");
                    }
                    // we need to move outward in the tree
                    // so we can read the next array element
                    // so we skip the rest of this object
                    reader.SkipToEndObject();
                    if (reader.Read())                     // read past the end of the object
                    {
                        reader.SkipToField("name");
                        // we're currently on the *key*/field name
                        if (reader.Read())
                        {
                            Console.WriteLine(reader.ParseSubtree());
                        }
                        else                         // below should never execute
                        {
                            Console.WriteLine("Sanity check failed, key has no value");
                        }
                    }
                    else                     // below should never execute, we didn't expect to reach the end
                    {
                        Console.WriteLine("Sanity check failed, unexpected end of document");
                    }
                }
                else
                {
                    Console.WriteLine("Not found");
                }
            }
            return;

            using (var reader = JsonTextReader.CreateFromUrl("http://api.themoviedb.org/3/tv/2129?api_key=c83a68923b7fe1d18733e8776bba59bb"))
            {
                // skip to "$.created_by[1].name" <-- JSON path syntax
                if (reader.SkipTo("created_by", 1, "name"))
                {
                    // we're currently on the *key*/field name
                    // we have to move to the value if we want 
                    // just that.
                    if (reader.Read())
                    {
                        Console.WriteLine(reader.ParseSubtree());
                    }
                    else                     // below should never execute
                    {
                        Console.WriteLine("Sanity check failed, key has no value");
                    }
                }
                else
                {
                    Console.WriteLine("Not found");
                }
            }
            return;

            using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
            {
                while (reader.Read())
                {
                    Console.Write(reader.NodeType);
                    if (JsonNodeType.Value == reader.NodeType ||
                        JsonNodeType.Key == reader.NodeType)
                    {
                        Console.Write(" " + reader.Value);
                    }
                    Console.WriteLine();
                }
            }
            return;

            using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
            {
                if (reader.SkipTo("seasons", 7, "episodes", 3, "guest_stars", 0, "character"))
                {
                    // move past the field name if it's a key.
                    if (JsonNodeType.Key != reader.NodeType || reader.Read())
                    {
                        Console.WriteLine(reader.ParseSubtree());
                    }
                }
            }
            // or...
            using (var reader = JsonTextReader.CreateFrom(@"..\..\data.json"))
            {
                if (reader.SkipToField("created_by"))
                {
                    if (reader.SkipToIndex(0))
                    {
                        reader.Read();
                        if (reader.SkipToField("name"))
                        {
                            if (reader.Read())
                            {
                                Console.WriteLine(reader.Value);
                            }
                        }
                    }
                }
            }

            return;

            Tmdb.ApiKey     = ApiKey;
            Tmdb.CacheLevel = JsonRpcCacheLevel.Aggressive;

            // hit it *hard* - 10 pages of movies, 10 of TV
            foreach (var movie in TmdbMovie.GetTopRated(0, 9))
            {
                _RunMovieDemo(movie.Id);
            }

            foreach (var show in TmdbShow.GetTopRated(0, 9))
            {
                _RunShowDemo(show.Id);
            }
        }