如何使用变量定义构造try / catch

时间:2014-01-23 06:38:50

标签: c# json try-catch json.net deserialization

用C#编写,我在这里得到JSONReaderExceptions:

var container = JsonConvert.DeserializeObject<HistoryResponseContainer> (responseData);

它总是像未终止的线或无法识别的角色。

我知道我需要捕获并抛出这些错误,但如果我尝试在try / catch语句中定义“container”,它就会中断。这是整个方法。

foreach (String StationID in StationIDList) { 

            string url = @"http://api.wunderground.com/api/" + wundergroundkey + "/history_" + Date + "/q/pws:" + StationID + ".json";
            Uri uri = new Uri (url);
            WebRequest webRequest = WebRequest.Create (uri);
            WebResponse response = webRequest.GetResponse ();
            StreamReader streamReader = new StreamReader (response.GetResponseStream ());
            String responseData = streamReader.ReadToEnd ();

            var container = JsonConvert.DeserializeObject<HistoryResponseContainer> (responseData);

            foreach (var observation in container.history.observations) {

                CurrentData.Write (StationID + " ");
                // This makes easier access to the date. not perfect, but better.
                DateTime date = observation.date.Value;
                DateTime utc = observation.utcdate.Value;

                // whatever you want to do with each observation
                if (date.Minute == 0 || date.Minute % 5 == 0) {
                    CurrentData.Write (date.Hour + ":" + date.Minute + " " + observation.wdird + " " + observation.wspdi);
                }//end if

                CurrentData.Write ("\n");

            } //End foreach observation

        } //end foreach station

2 个答案:

答案 0 :(得分:2)

将你的第二个foreach循环置于try(因为你正在使用你的“容器”)。否则,如果引发异常并且您只是“尝试/捕获”容器实例,则不会定义它:

foreach (String StationID in StationIDList) 
{ 
    string url = @"http://api.wunderground.com/api/" + wundergroundkey + "/history_" + Date + "/q/pws:" + StationID + ".json";
    Uri uri = new Uri (url);
    WebRequest webRequest = WebRequest.Create (uri);
    WebResponse response = webRequest.GetResponse ();
    StreamReader streamReader = new StreamReader (response.GetResponseStream ());
    String responseData = streamReader.ReadToEnd ();

    try
    {
        var container = JsonConvert.DeserializeObject<HistoryResponseContainer> (responseData);

        foreach (var observation in container.history.observations) 
        {
            CurrentData.Write (StationID + " ");
            DateTime date = observation.date.Value;
            DateTime utc = observation.utcdate.Value;

            if (date.Minute == 0 || date.Minute % 5 == 0) 
            {
                CurrentData.Write (date.Hour + ":" + date.Minute + " " + observation.wdird + " " + observation.wspdi);
            }

            CurrentData.Write ("\n");

        }
    }
    catch(JsonReaderException ex)
    {
        // ...
    }
}

答案 1 :(得分:0)

  

我知道我需要抓住并抛出这些错误,

不,你需要抓住并处理这些错误。

  

但如果我尝试在try / catch语句中定义“容器”,它就会呕吐。

你在这里是什么意思?你需要这个:

try
{
   // your code that throws exception
}
catch(JsonReaderException ex)
{
   // handle your exception
}