Example #1
0
        private List <WeatherItem> JsonParser(string json)
        {
            var items = new List <WeatherItem>();

            var obj = JObject.Parse(json);

            obj = (JObject)obj["response"];
            if ((string)obj["header"]["resultCode"] != "0000")
            {
                items.Add(new WeatherItem()
                {
                    Success = false
                });
                return(items);
            }

            var dataArray = (JArray)obj["body"]["items"]["item"];

            var dataSet = new List <WeatherData>();

            foreach (var data in dataArray)
            {
                dataSet.Add(new WeatherData()
                {
                    Category  = (string)data["category"],
                    FcstDate  = (string)data["fcstDate"],
                    FcstTime  = (string)data["fcstTime"],
                    FcstValue = (string)data["fcstValue"]
                });
            }

            var sortedData = new List <List <WeatherData> >();

            while (dataSet.Count != 0)
            {
                var dataSetA = dataSet.Where(x => x.FcstTime == dataSet.First().FcstTime).ToList();
                dataSetA.RemoveAll(x => x.FcstDate != dataSetA.First().FcstDate);

                for (int i = 0; i < dataSetA.Count; i++)
                {
                    dataSet.Remove(dataSetA[i]);
                }

                sortedData.Add(dataSetA);
            }

            if (sortedData.Count > 6)
            {
                sortedData.RemoveRange(6, sortedData.Count - 6);
            }

            foreach (var data in sortedData)
            {
                var result = new WeatherItem()
                {
                    Success      = true,
                    BaseDateTime = WeatherConverter.ConvertDateTime(data.First().FcstDate, data.First().FcstTime)
                };

                int pty = 0;
                int sky = 0;
                foreach (var item in data)
                {
                    switch (item.Category)
                    {
                    case "POP":
                        result.POP.Value = Convert.ToInt32(item.FcstValue);
                        break;

                    case "R06":
                        result.R06.Value = Convert.ToDouble(item.FcstValue);
                        break;

                    case "REH":
                        result.REH.Value = Convert.ToInt32(item.FcstValue);
                        break;

                    case "S06":
                        result.S06.Value = Convert.ToInt32(item.FcstValue);
                        break;

                    case "T3H":
                        result.T3H.Value = Convert.ToDouble(item.FcstValue);
                        break;

                    case "WSD":
                        result.WSD.Value = Convert.ToDouble(item.FcstValue);
                        break;

                    case "PTY":
                        pty = Convert.ToInt32(item.FcstValue);
                        break;

                    case "SKY":
                        sky = Convert.ToInt32(item.FcstValue);
                        break;

                    default:
                        break;
                    }
                }

                result.SkyStat.Value = Sky.GetSkyCode(sky, pty);
                result.SkyStat.Icon  = WeatherIcon.GetWeatherIcon(result.SkyStat.Value, result.BaseDateTime.Hour);

                items.Add(result);
            }

            return(items);
        }