SkyDrive へ移動※ VS2010 用 ■ テキストファイル(SHIFT_JIS)の内容を OpenFileDialog で開いて読み込む ■ 読み込んだテキストをツイートする ■ ツイートの結果の JSON 内の日本語を元に戻してインデント整形して UTF8N で出力 ■ ツイートの結果の JSON を定義したクラスのインスタンスに一括投入後インデント整形して UTF8N で出力 以上の処理を リツイートに対しても行います
JSON の処理は、Json.NET を使用しています(テンプレートに同梱) 1) 整形
JObject jo = JObject.Parse(json); string json_work = JsonConvert.SerializeObject(jo, Formatting.Indented);2) デシリアライズと整形TweetResult tr = JsonConvert.DeserializeObjectProgram.cs(json); json_work = JsonConvert.SerializeObject(tr, Formatting.Indented); ----- private class TweetResult { public string created_at { get; set; } public string id { get; set; } public string text { get; set; } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Reflection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace TryApp_Tweet1 { public class Program { // 外部テキストファイルを読み込んだ内容 static string text = "まだ読み込まれていません"; static string json = "まだセットされていません"; [STAThreadAttribute] static void Main(string[] args) { App app = new App(); // イベントの追加 app.ShowEnv += MyProc1; app.GetDir += (string path) => { Console.WriteLine(path + " が選択されました"); string[] filenames = null; if (path != "") { filenames = Directory.GetFiles(path); foreach (string value in filenames) { Console.WriteLine(value); } } }; // ****************************************** // ファイルを選択 // ****************************************** app.GetFile += (string path) => { // 外部ファイルが SHIFT_JIS で書かれているという前提で読み込む StreamReader reader = new StreamReader(path, Encoding.GetEncoding("shift_jis")); // 一気に全て読み込み text = reader.ReadToEnd(); reader.Close(); Console.WriteLine(path + " が読み込まれました"); }; // ****************************************** // テキストを表示 // ****************************************** app.DisplayText += (string cmd) => { Console.WriteLine(text); }; // ****************************************** // JSONを表示 // ****************************************** app.DisplayJson += (string cmd) => { JObject jo = JObject.Parse(json); string json_work = JsonConvert.SerializeObject(jo, Formatting.Indented); Console.WriteLine(json_work); }; // ****************************************** // ツイート処理の登録 // ****************************************** // メソッドによる実装 app.Cmd += Tweet; // ラムダ式による実装 app.Cmd += (string cmd) => { Console.WriteLine(cmd + " が実行されました"); }; // ****************************************** // 終了せずに、コマンドプロンプトを保持する。 // ( "q" で終了 ) // ****************************************** app.Loop(); } // ****************************************** // 環境周りの情報 // ****************************************** static private void MyProc1(string[] data) { foreach (string value in data) { Console.WriteLine(value); } } // ****************************************** // 処理のテンプレート // ****************************************** static private void Tweet(string cmd) { VS2010_Twitter twitter = new VS2010_Twitter( "Consumer key", "Consumer secret", "Access token", "Access token secret" ); switch (cmd) { case "tweet": // 投稿 json = twitter.Tweet(text); break; // retweet:id でリツイート default: // id を取得する為に、":" で配列に分解 string[] param = cmd.Split(new[] { ":" }, StringSplitOptions.None); // id でリツイート json = twitter.ReTweet(param[1]); break; } // 取得した JSON を utf8n で書き込む string thisPath = Assembly.GetExecutingAssembly().Location; string thisDir = Path.GetDirectoryName(thisPath); string path = thisDir + @"\" + "json.txt"; StreamWriter writer = new StreamWriter(path, false, new UTF8Encoding(false)); // JSON の日本語表示を元に戻して整形 JObject jo = JObject.Parse(json); string json_work = JsonConvert.SerializeObject(jo, Formatting.Indented); // 書き込み writer.Write(json_work); writer.Close(); // オブジェクトに直接投入 TweetResult tr = JsonConvert.DeserializeObject<TweetResult>(json); Console.WriteLine(tr.created_at); Console.WriteLine(tr.id); Console.WriteLine(tr.text); // クラスに投入した内容を出力 path = thisDir + @"\" + "json2.txt"; writer = new StreamWriter(path, false, new UTF8Encoding(false)); json_work = JsonConvert.SerializeObject(tr, Formatting.Indented); // 書き込み writer.Write(json_work); writer.Close(); } private class TweetResult { public string created_at { get; set; } public string id { get; set; } public string text { get; set; } } } }
App.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.IO; using System.Windows.Forms; namespace TryApp_Tweet1 { class App { // 引数が一つのイベント public delegate void AppHandler(string cmd); // 引数が文字列配列のイベント public delegate void AppHandlerResults(string[] data); // 環境周りの情報を取得するイベント public event AppHandlerResults ShowEnv = null; // フォルダ選択ダイアログ public event AppHandler GetDir = null; // ファイル選択ダイアログ public event AppHandler GetFile = null; // イベントのテンプレート public event AppHandler Cmd = null; // 追加イベント public event AppHandler DisplayText = null; public event AppHandler DisplayJson = null; // ****************************************** // コンストラクタ // ****************************************** public App() { } // ****************************************** // 終了せずに、コマンドプロンプトを保持する。 // ****************************************** public void Loop() { while (true) { // ****************************************** // 専用プロンプト // ****************************************** Console.Write("app>"); string line = Console.ReadLine(); // ****************************************** // 終了コマンド // ****************************************** if (line == "q") { break; } // ****************************************** // 環境周りの情報を取得するイベント // ****************************************** if (line == "env") { if (ShowEnv != null) { // 実行ファイル string thisPath = Assembly.GetExecutingAssembly().Location; // 実行ファイルのあるディレクトリ string thisDir = Path.GetDirectoryName(thisPath); string[] result = { line, thisPath, thisDir }; ShowEnv(result); } } // ****************************************** // フォルダ選択 // ****************************************** if (line == "dir") { if (GetDir != null) { // フォルダ選択ダイアログ var dir = new FolderBrowserDialog(); dir.SelectedPath = @"c:\"; var result = dir.ShowDialog(); string path = ""; if (result == DialogResult.OK) { path = dir.SelectedPath; } GetDir(path); } } // ****************************************** // フアイル選択 // ****************************************** if (line == "file") { if (GetFile != null) { // ファイル選択ダイアログ var file = new OpenFileDialog(); file.Filter = "全て|*.*|テキスト|*.txt;*.log"; file.FilterIndex = 0; file.FileName = ""; var dr = file.ShowDialog(); string path = ""; if (dr == DialogResult.OK) { path = file.FileName; } GetFile(path); } } // ****************************************** // イベントのテンプレート // ****************************************** if (line == "tweet" || (line + " ").Substring(0, 7) == "retweet") { if (Cmd != null) { Cmd(line); } } // ****************************************** // 追加のイベント // ****************************************** if (line == "text") { if (DisplayText != null) { DisplayText(line); } } if (line == "json") { if (DisplayJson != null) { DisplayJson(line); } } } } } }
関連する記事 ■ VS2010_Twitter.cs ■ アプリケーションテスト用コンソールアプリ(TryApp)テンプレート
|
【VS(C#)の最新記事】
- Replit : cs-list
- C# : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- C#( Form ) : ウインドウ枠の無い吹き出しの作成
- C# のタプル( Visual Studio 2017 でテスト )
- C# : インターネット上の JSON ファイルのフォーマットを クラスとして定義して1行でオブジェクト化して使用する
- C# の文法的文字列処理
- C# : System.Data.Odbc によるデータベースのテーブルからのデータ取得処理( サンプルの SQL は MySQL 用です )
- C# : Excel を データベースとして DataGridView に読み込む
- C# : dynamic 型 による Excel へのアクセス
- C# : フォームを表示せずに、通知領域にアイコンを表示させる常駐プログラム
- Microsoft Access に対してSQLを入力してその結果を DataGridView に表示する最も簡単なコード
- C# : System.Data.Odbc データ取得(SELECT)処理( MySQL ) : ※ using 無し( Dispose 実行 )
- C# : SQL 文を外部テキストにして、String.Format でデータ部分を置き換えて利用する
- C# コンソールアプリを AN HTTPD で実行
- C# : SQLServer( SQLExpress ) の SMO を使用してテーブルの CREATE TABLE 文 を取得する
- C# : DataGridView に TKMP.DLL の IMAP(POP3) で受信したメールを非同期に表示する( 添付ファイルも取得 )
- C# : TKMP.DLLを使った、メール送信テンプレート
- C# と VB.net : TKMP.DLL を使って IMAP でメール本文の一覧を取得する( コンソール )
- C# でDataTable と DataSource を使用して、DataGridView にデータを表示するテンプレート( 行をダブルクリックしてダイアログを表示して行データを処理 )
- (C#) / VS2010 または VS2012 : TKMP.DLL(3.1.2 または 3.1.8)を使った、『さくらインターネット』用メール送信テンプレート