从 JSON 数据内的特定字段中删除单引号字符

下面有这个 json 数据字符串,我需要对其进行一些清理,然后才能反序列化为 C# 中的对象。这是我的 json 字符串:

{'data':[
    {'ID':'01','Name':'Name 1','Description':'abc','Skills':[{'Type':'abc','Technical':'abc','Description':'abc'}],'Status':false,'Inactive':0},
    {'ID':'02','Name':'Name 2','Description':'abc','Skills':[{'Type':'abc','Technical':'abc','Description':'abc'}],'Status':false,'Inactive':0},
    {'ID':'03','Name':'Name 3','Description':'abc','Skills':[{'Type':'abc','Technical':'abc','Description':'abc'}],'Status':false,'Inactive':1}]}

我想要做的是从上述数据的以下字段中删除单引号(')字符:

'Skills':[{'Type':'abc','Technical':'abc','Description':'abc'}]

所以我需要实现的是让“技能”字段看起来像这样:

'Skills':[{Type:abc,Technical:abc,Description:abc}]

我设计了这个正则表达式模式:

(?<='Skills':\[\{)(.*?)(?=\}\],)

它与下面的字符串匹配,但我不知道如何排除单引号。

'Type':'abc','Technical':'abc','Description':'abc'

有人可以帮忙吗?


动漫人物
浏览 156回答 1
1回答

拉风的咖菲猫

最好修改源以获得格式漂亮的 JSON,它不是标准 JSON 格式。如果您无权修改源输出,可以使用以下命令:&nbsp; &nbsp; &nbsp; &nbsp; string content = Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; var matchResult = new Regex("(?<='Skills':).*?}]").Matches(content);&nbsp; &nbsp; &nbsp; &nbsp; foreach(Match match in matchResult)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string matchValueWithoutSingleQuote = match.Value.Replace("'", string.Empty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content = content.Replace(match.Value, matchValueWithoutSingleQuote);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(content);&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();输出是:{'data':[&nbsp; &nbsp; {'ID':'01','Name':'Name 1','Description':'abc','Skills':[{Type:abc,Technical:abc,Description:abc}],'Status':false,'Inactive':0},&nbsp; &nbsp; {'ID':'02','Name':'Name 2','Description':'abc','Skills':[{Type:abc,Technical:abc,Description:abc}],'Status':false,'Inactive':0},&nbsp; &nbsp; {'ID':'03','Name':'Name 3','Description':'abc','Skills':[{Type:abc,Technical:abc,Description:abc}],'Status':false,'Inactive':1}]}林克版本:string content = Console.ReadLine();var matchResult = new Regex("(?<='Skills':).*?}]").Matches(content);var jsonWithNormalizedSkillField = matchResult.Cast<Match>().Select(s => content.Replace(s.Value, s.Value.Replace("'", string.Empty))).FirstOrDefault();Console.WriteLine(jsonWithNormalizedSkillField);Console.ReadLine();
打开App,查看更多内容
随时随地看视频慕课网APP