How to handle null/empty values in JsonConvert.DeserializeObject

后端 未结 4 710
执笔经年
执笔经年 2020-12-12 22:52

I have the following code:

return (DataTable)JsonConvert.DeserializeObject(_data, (typeof(DataTable)));

Then, I tried:

var          


        
4条回答
  •  死守一世寂寞
    2020-12-12 23:13

    You can subscribe to the 'Error' event and ignore the serialization error(s) as required.

        static void Main(string[] args)
        {
            var a = JsonConvert.DeserializeObject("-- JSON STRING --", new JsonSerializerSettings
            {
                Error = HandleDeserializationError
            });
        }
    
        public static void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
        {
            var currentError = errorArgs.ErrorContext.Error.Message;
            errorArgs.ErrorContext.Handled = true;
        }
    

提交回复
热议问题