首页 技术 正文
技术 2022年11月6日
0 收藏 312 点赞 376 浏览 794 个字

使用Newtonsoft.Json反序列化收到的字串为JObject或其它支持的数据模型,有时错误,提示如下:

Newtonsoft.Json反序列化(Deserialize)出错:Bad JSON escape sequence

Bad JSON escape sequence: \c. Path 'idno', line , position .

甚纳闷之。遂搜索资料,略有小获,其非法分界符所致。合法的分隔符为:

Newtonsoft.Json反序列化(Deserialize)出错:Bad JSON escape sequence

以此为依据,对字串做正则替换,问题解决,录代码以记之。

        static void Main(string[] args)
{
string s = @"
{
""name"": ""王艳"",
""sex"": ""女"",
""idno"": ""34*****\c0"",
""addr"": ""安徽省"",
""telephone"": ""no number"",
""thumbnail"": ""e:\docs\person\thumbnail\wy.jpg"",
""Age"": 27,
""DeptName"": ""姚江\R中心""
}
"; //\加bfrnt\/'"为合法分隔符,其它不是,替换
string pattern = @"(\\[^bfrnt\\/'\""])";
s = Regex.Replace(s, pattern, "\\$1");
var jo = JsonConvert.DeserializeObject<JObject>(s);
Console.WriteLine(jo.ToString()); Console.ReadLine();
}

结果如图:

Newtonsoft.Json反序列化(Deserialize)出错:Bad JSON escape sequence

参考资料:

How to escape special characters in building a JSON string? – Stack Overflow

Bad JSON escape sequence: \l. Path 'Transforms[0].Path', line 3, position 45. · Issue #343 · mmanela/chutzpah

Newtonsoft.Json高级用法 – 焰尾迭 – 博客园

上一篇: fabric 持久化
下一篇: Scrapy反爬
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,035
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,521
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,369
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,150
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,783
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,864