public StockModel Execute(string jsonResult)
        {
            var        jsonContent = JsonConvert.DeserializeObject <JObject>(jsonResult);
            JToken     subJson;
            StockModel stockModel = null;

            //避免代碼為上市但查上櫃
            if (jsonResult.IndexOf("\"d\"") < 0)
            {
                return(stockModel);
            }

            foreach (var item in jsonContent)
            {
                subJson = item.Value;
                var child = subJson.Children();

                foreach (JToken token in child)
                {
                    var oo = token.Root.ToString();
                    stockModel             = JsonConvert.DeserializeObject <StockModel>(oo);
                    stockModel.NowDateTime = ParseMilisecond(stockModel.msgArray[0].Miliseconds.ToString());
                    break;
                }

                if (stockModel != null)
                {
                    break;
                }
            }
            return(stockModel);
        }
예제 #2
0
        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine("Give me number ...");

                SetWindowSize();

                string input = Console.ReadLine();

                if (string.IsNullOrEmpty(input))
                {
                    Environment.Exit(0);
                }
                else
                {
                    if (input.Length > 4)
                    {
                        Console.WriteLine("Wrong Code");
                    }

                    StockInfoReader  reader    = new StockInfoReader();
                    List <StockData> modelData = new List <StockData>();
                    //string content = reader.ReadJSONFromFile(@"D:\stock.json");
                    string url = string.Format(
                        "http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_{0}.tw",
                        input);
                    string content;
                    content = reader.ReadJSONFromUrl(url);
                    StockModel stockModel = reader.Execute(content);

                    if (stockModel == null)
                    {
                        url = string.Format(
                            "http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=otc_{0}.tw",
                            input);
                        content    = reader.ReadJSONFromUrl(url);
                        stockModel = reader.Execute(content);
                    }
                    if (stockModel == null)
                    {
                        Console.WriteLine("********Code:{0} No Data********", input);
                    }
                    else
                    {
                        modelData = (stockModel == null ? new List <StockData>() : stockModel.msgArray);
                        StockData data = modelData[0];

                        Console.WriteLine("************Code:{0}***********", data.Code); Console.WriteLine("H:{0}  , L:{1}", data.High.ToString("##.##"), data.Low.ToString("##.##"));
                        Console.WriteLine("O:{0}  , C:{1} , Sbt:{2}",
                                          data.Open.ToString("##.##"), data.Current.ToString("##.##"), ((data.Current - data.Open) * 1000).ToString("##.##"));
                        Console.WriteLine("Now:{0}", stockModel.NowDateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        Console.WriteLine("*************END************");
                    }

                    Console.WriteLine();
                }
            }while (true);
        }