C# 去除json字符串key引号

时间:2022-09-15 12:06:24

采用正则表达式去除;

方法

        /// <summary>
/// 去除json key双引号
/// </summary>
/// <param name="jsonInput">json</param>
/// <returns>去除key引号</returns>
public string JsonRegex(string jsonInput)
{
string result=string.Empty;
try
{
string pattern = "\"(\\w+)\"(\\s*:\\s*)";
string replacement = "$1$2";
System.Text.RegularExpressions.Regex rgx
= new System.Text.RegularExpressions.Regex(pattern);
result
= rgx.Replace(jsonInput, replacement);
}
catch (Exception ex)
{
result
= jsonInput;
}
return result;
}

测试案例:

json:{"action_r": [{"spr": "1", "mas": "21", "mgt": "入职"}, {"spr": "1", "mas": "22", "mgt": "转岗"}]}

public string PA_Test()
{
string str = "{\"action_r\": [{\"spr\": \"1\", \"mas\": \"21\", \"mgt\": \"入职\"}, {\"spr\": \"1\", \"mas\": \"22\", \"mgt\": \"转岗\"}]}";
str
= this.JsonRegex(str);

return str;
}

返回结果:

{action_r: [{spr: "1", mas: "21", mgt: "入职"}, {spr: "1", mas: "22", mgt: "转岗"}]}

 

 

 

参考:

http://bbs.csdn.net/topics/330267907

http://www.cnblogs.com/cexm/p/6322011.html

http://www.jb51.net/article/21529.htm

http://blog.csdn.net/graceMeMe/article/details/37562327?locationNum=9