C# Newtonsoft.Json.JsonReaderException:“Could not convert string to decimal:

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Newtonsoft.Json.JsonReaderException:“Could not convert string to decimal:相关的知识,希望对你有一定的参考价值。

使用Newtonsoft.Json,报以上错误,问题的原因是有"",把“”替换成null:

以前的json:

[{"WengvNj":"df5c38c6dd1744c59605da1fc85a0500","WengvSgtm":"2019-12-16 修理合同","WengvSu":"2019TJR001LX","WengvYewm":"修理","WengvXzhbdc":"1","WengvSuam":"","WengvXonvmkeq":"2019/12/16 0:00:00","WengvKosqjvp":"GK","WengvHapl":"SVH30","WengvWgaqoijz":"Y","WengvWgaqoiyags":"OK,同意!","WengvWgaqoixma":"wecrew","WengvWgaqoiomgs":"2019/12/16 10:20:10","WengvUrhvmkeqfhphk":"2019/12/16 0:00:00","WengvFyrbjuppnht":"2020/2/13 0:00:00","WengvWkhtrdjpnhtikskn":"2019/12/16 0:00:00","WengvWkhtrdjfnyttrlx":"","WengvJslz":"0","WengvYxhvbzzdg":"1","WengvUgfunxeoba":"","WengvUgfunxeyrh":"0","WengvUgfunxeegoikj":"","WengvJdjpjxrq":"","WengvHayznxnk":"CNY","WengvUgfunxeeha":"","WengvXat":"4400","WengvFmlvli":"LMF","WengvYxhlndjbr":"EXW","WengvXvlkrkwzbht":"","WengvHxlicoxma":"wecrew","WengvHxlicoomgs":"2019/12/16 10:19:46","WengvQgzbmkeq":"","WengvAgsqm":"","WengvTxkmanlfr":"","WengvFoyuxnp":"","WengvGafma":"","WengvGafmazsaas":"","WengvGafmaoxmvz":"","WengvAkulxb":"","WengvAkulxbtzicxsvswx":"","WengvAkulxbdtvdptu":"","WengvAkulxblpq":"","WengvAkulxbwuaybqe":"","WengvAkulxbwuayexffx":"","WengvAkulxbwuaytcrae":"","WengvGafmavtzxapd":"","WengvGafmavtzxdweew":"","WengvGafmavtzxsbqzd":"","WengvGafmamzzswgc":"","WengvGafmakabeckui":"","WengvGafmamzzswgcusmy":"","WengvAkulxbnaatxhd":"","WengvAkulxbdutbieiq":"","WengvAkulxbnaatxhdvtnz":"","WengvHuubaknfgmeu":"","WengvXkulyoamvfptu":"","WengvXkulyoamvfayec":"","WengvXkulyoamvfayecibjjb":"","WengvWkwirbxmaost":"","WengvWkwirbxmazxdb":"","WengvWkwirbxmazxdbhaiia":"","WengvXkulyoamvfhyxfmimu":"","WengvXkulyoamvfhyxfmimuayse":"","WengvWkwirbdutbieiq":"","WengvWkwirbdutbieiqwuoa":""}]

  解决方案:

        public string RemoveJsonFieldIsNull(string str)
        {
            if (string.IsNullOrEmpty(str)) return str;
            string reg=":""";
            str = ReplaceByRegex(reg, str, ":null");

            return str;

            //string reg = "(?<beginStr>[^"]*?)(?<key>"?We*?"?:?)(?<value>".*?",*)(?<endStr>[^"]*?)";
            //string strSrcReg = "(?<beginSrc>^.*?(?=images))(?<char>(images))(?<endSrc>(?<=images).*?$)";
           // string reg = "(?<beginStr>^.*?)(?=:"")(?<value>(:""))(?<endStr>(?<=:"").*?$)";
            //string reg = "(?<beginStr>^.*?)(?<value>(:""))(?<endStr>.*?$)";
            //return ReplaceByRegex(reg, str, match =>
            //{
            //    string beginStr = match.Groups["beginStr"].Value;
            //    string value = match.Groups["value"].Value;
            //    string endStr = match.Groups["endStr"].Value;
            //    return beginStr+ ":null"+ endStr;
            //});
        }
        public static string ReplaceByRegex(string strReg, string html, string target)
        {
            return ReplaceByRegex(strReg, html, m => target);
        }
        public static string ReplaceByRegex(string strReg, string html, MatchEvaluator function)
        {
            if (string.IsNullOrEmpty(html) || string.IsNullOrEmpty(strReg)) return null;

            Regex regex = new Regex(strReg, RegexOptions.Singleline | RegexOptions.IgnoreCase);

            html = regex.Replace(html, function);
            return html;
        }

  

//去掉为json中value为null的key
            var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

  处理后的json:

[{"WengvNj":"df5c38c6dd1744c59605da1fc85a0500","WengvSgtm":"2019-12-16 修理合同","WengvSu":"2019TJR001LX","WengvYewm":"修理","WengvXzhbdc":"1","WengvSuam":null,"WengvXonvmkeq":"2019/12/16 0:00:00","WengvKosqjvp":"GK","WengvHapl":"SVH30","WengvWgaqoijz":"Y","WengvWgaqoiyags":"OK,同意!","WengvWgaqoixma":"wecrew","WengvWgaqoiomgs":"2019/12/16 10:20:10","WengvUrhvmkeqfhphk":"2019/12/16 0:00:00","WengvFyrbjuppnht":"2020/2/13 0:00:00","WengvWkhtrdjpnhtikskn":"2019/12/16 0:00:00","WengvWkhtrdjfnyttrlx":null,"WengvJslz":"0","WengvYxhvbzzdg":"1","WengvUgfunxeoba":null,"WengvUgfunxeyrh":"0","WengvUgfunxeegoikj":null,"WengvJdjpjxrq":null,"WengvHayznxnk":"CNY","WengvUgfunxeeha":null,"WengvXat":"4400","WengvFmlvli":"LMF","WengvYxhlndjbr":"EXW","WengvXvlkrkwzbht":null,"WengvHxlicoxma":"wecrew","WengvHxlicoomgs":"2019/12/16 10:19:46","WengvQgzbmkeq":null,"WengvAgsqm":null,"WengvTxkmanlfr":null,"WengvFoyuxnp":null,"WengvGafma":null,"WengvGafmazsaas":null,"WengvGafmaoxmvz":null,"WengvAkulxb":null,"WengvAkulxbtzicxsvswx":null,"WengvAkulxbdtvdptu":null,"WengvAkulxblpq":null,"WengvAkulxbwuaybqe":null,"WengvAkulxbwuayexffx":null,"WengvAkulxbwuaytcrae":null,"WengvGafmavtzxapd":null,"WengvGafmavtzxdweew":null,"WengvGafmavtzxsbqzd":null,"WengvGafmamzzswgc":null,"WengvGafmakabeckui":null,"WengvGafmamzzswgcusmy":null,"WengvAkulxbnaatxhd":null,"WengvAkulxbdutbieiq":null,"WengvAkulxbnaatxhdvtnz":null,"WengvHuubaknfgmeu":null,"WengvXkulyoamvfptu":null,"WengvXkulyoamvfayec":null,"WengvXkulyoamvfayecibjjb":null,"WengvWkwirbxmaost":null,"WengvWkwirbxmazxdb":null,"WengvWkwirbxmazxdbhaiia":null,"WengvXkulyoamvfhyxfmimu":null,"WengvXkulyoamvfhyxfmimuayse":null,"WengvWkwirbdutbieiq":null,"WengvWkwirbdutbieiqwuoa":null}]

  

 

以上是关于C# Newtonsoft.Json.JsonReaderException:“Could not convert string to decimal:的主要内容,如果未能解决你的问题,请参考以下文章

C#进阶C# 泛型

C#进阶C# 匿名方法

C#进阶C# 多线程

C# 教程

[C#教程01]C# 简介

用 C# 编写 C# 编译器,先有鸡还是先有蛋?