需求來源
這兩天做一個拍賣網站的項目,偶爾會有一些單詞想不起來,順手花五分鐘就寫了一個查單詞的,命令行簡單點就它了。
class result { //{"from":"en","to":"zh","trans_result":[{"src":"find","dst":"/u627e/u5230"}]} public string from { get; set; } public string to { get; set; } public trans_result[] trans_result { get; set; } } class trans_result { public string src { get; set; } public string dst { get; set; } } class PRogram { private static String DecodeUnicode(String dataStr) { Regex reg = new Regex(@"(?i)//[uU]([0-9a-f]{4})"); return reg.Replace(dataStr, delegate(Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); }); } static void Main(string[] args) { if (args.Length>0) { string s1 = args[0].ToString(); string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", "這里填寫百度appid", s1, "auto", "auto"); WebClient wc = new WebClient(); result r = JsonConvert.DeserializeObject<result>(wc.DownloadString(url)); Console.WriteLine(DecodeUnicode(r.trans_result[0].dst)); Console.WriteLine("......"); } else { while (true) { string s = Console.ReadLine(); if (s == "over" || s == "quit" || s == "exit") { break; } string url = string.Format("http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}", "這里填寫百度appid", s, "auto", "auto"); WebClient wc = new WebClient(); result r = JsonConvert.DeserializeObject<result>(wc.DownloadString(url)); Console.WriteLine(DecodeUnicode(r.trans_result[0].dst)); Console.WriteLine("......"); } } Console.WriteLine("退出查單詞,感謝使用!"); } }
沒什么技術難度,方便生活而已,最后想在命令行直接調用,別忘配置path環境變量,大約就是醬
新聞熱點
疑難解答