Program.cs :  » Development » Jayrock » YahooJsonNewsReader » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » Jayrock 
Jayrock » YahooJsonNewsReader » Program.cs
namespace YahooJsonNewsReader{
    #region Imports

    using System;
    using System.IO;
    using System.Net;
    using Jayrock.Json;

    #endregion

    /// <summary>
    /// This program demonstrates using JsonTextReader to parse JSON text in 
    /// a forward-only, stream-oriented fashion.
    /// </summary>

    internal static class Program
    {
        private static void Main(string[] args)
        {
            const int keywordsLimit = 70;
            
            Console.WriteLine("Yahoo (JSON) News Reader");
            Console.WriteLine();
            Console.WriteLine("When prompted, enter keywords (e.g. Ray Charles) to search");
            Console.WriteLine("news for. Program ends if an empty keyword list is supplied.");

            WebClient client = new WebClient();
            string urlFormat = args.Length > 0 ? args[0].Trim() : string.Empty;

            if (string.IsNullOrEmpty(urlFormat))
            {
                urlFormat = "http://search.yahooapis.com/NewsSearchService/V1/newsSearch?" +
                    "appid=YahooDemo&query={0}&results=10&language=en&output=json";
            }

            while (true)
            {
                Console.WriteLine();
                Console.Write("Keywords: ");

                string keywords = Console.ReadLine().Trim();

                if (keywords.Length == 0)
                    return;

                if (keywords.Length > keywordsLimit)
                {
                    Console.WriteLine("Keywords exceed {0:N0} chars! Try again.", keywordsLimit);
                    continue;
                }

                Console.WriteLine();
                Console.Write("Downloading news...");

                string escapedKeywords = Uri.EscapeUriString(keywords);
                Uri url = new Uri(string.Format(urlFormat, escapedKeywords));
                string news = client.DownloadString(url);

                Console.WriteLine("Done!");
                Console.WriteLine();

                //
                // Read through the entire JSON response and display the
                // string value of object member named "Title". The
                // shape of the news data looks something like this:
                //
                //  { 
                //      "ResultSet" : {
                //          "totalResultsAvailable" : ...,
                //          "totalResultsReturned" : ...,
                //          "firstResultPosition": ...,
                //          "Result" : [ 
                //             {
                //                  "Title" : "..."
                //                 ...
                //              },
                //              ...
                //          ]
                //      }
                //  }
                //

                using (JsonTextReader reader = new JsonTextReader(new StringReader(news)))
                {
                    while (reader.Read())
                    {
                        if (reader.Depth == 4 &&
                            reader.TokenClass == JsonTokenClass.Member && 
                            reader.Text == "Title")
                        {
                            reader.Read(/* member */);
                            Console.WriteLine(reader.ReadString());
                        }
                    }
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.