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

2021-01-24 02:14

阅读:554

标签:方案   str   move   regex   替换   instr   read   nbsp   ima   

使用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 = "(?[^\"]*?)(?\"?We*?\"?:?)(?\".*?\",*)(?[^\"]*?)";
            //string strSrcReg = "(?^.*?(?=images))(?(images))(?(?^.*?)(?=:\"\")(?(:\"\"))(?(?^.*?)(?(:\"\"))(?.*?$)";
            //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:

标签:方案   str   move   regex   替换   instr   read   nbsp   ima   

原文地址:https://www.cnblogs.com/sunliyuan/p/12058527.html


评论


亲,登录后才可以留言!