无效的参数异常

时间:2016-01-17 07:10:23

标签: c# json.net

我正在开发导航windows phone 8.1应用程序。我使用try catch块进行错误处理。一旦我向服务器发送无效输入,例如“dfkdsf”,它就会抛出一个运行时异常错误,指出Invalid Parameter但是catch块没有处理它。我哪里错了?

public async void GetSearchAPIData()
    {
        try
        {
            JArray arr = JArray.Parse(await responce.Content.ReadAsStringAsync());

            foreach (JObject obj in arr.Children<JObject>())    //exception thrown here!
            {
                noOfResult++;
                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;
                settings.MissingMemberHandling = MissingMemberHandling.Ignore;
                var rcvdData = JsonConvert.DeserializeObject<LocationData>(obj.ToString(), settings);
                //adding recieved data into a list..
                RcvdSearchDataList.Add(rcvdData);
            }
            //changing current status on status bar..
            statusBar.ProgressIndicator.Text = "Found " + noOfResult + " results for " + "\"" + UserRequestedLocation + "\"";
            statusBar.ProgressIndicator.ProgressValue = 0;

            if (RcvdSearchDataList.Count == 0)
            {
                statusBar.ProgressIndicator.Text = "We're Sorry! No matches found...";
                statusBar.ProgressIndicator.ProgressValue = 0;
            }
            else
            {
                ResultListView = new ListView() { Margin = new Thickness(20), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch };
                ResultDataGrid.Children.Add(ResultListView);
                for (int i = 0; i < RcvdSearchDataList.Count; i++)
                {
                    UpdateSearchTextData(i);
                }
            }
            UpdateSearchMapData(RcvdSearchDataList);
        }
        catch(Exception e)
        {
            string errorMessage = string.Format(
                    "An error occurred while looking for the requested location.\r\n\r\n{0:x}\r\n\r\n{1}", "HRESULT: " + e.HResult, "Message: " + e.Message);
            ErrorDialog.Content = errorMessage;
            await ErrorDialog.ShowAsync();
        }
    }

JArray arr从服务器返回值{[]},我无法处理

1 个答案:

答案 0 :(得分:0)

评论第settings.NullValueHandling = NullValueHandling.Ignore行。  你告诉Json Serializer在去血过程中忽略空值。 因此,当你得到空Json repsonse时,它没有反序列化它,并以某种方式导致无效的参数异常。