用C#正则表达式 补上KEY和值双引号
JSON字符串是:{MarketCode=总计,MarketName=总计,Site=,ResType={ResType=BCC},FrozenAmount=0,Free...
JSON字符串是:
{ MarketCode = 总计, MarketName = 总计, Site = , ResType = { ResType = BCC }, FrozenAmount = 0, FreeAmount = 0.0039963, LocalValue = 0, ReferValue = 0 }
要求结果是: { "MarketCode" = "总计", "MarketName" = "总计", "Site" ="" , "ResType" = "{ ResType = BCC }", "FrozenAmount" = 0, "FreeAmount" = 0.0039963, "LocalValue" = 0, "ReferValue" = 0 } 展开
{ MarketCode = 总计, MarketName = 总计, Site = , ResType = { ResType = BCC }, FrozenAmount = 0, FreeAmount = 0.0039963, LocalValue = 0, ReferValue = 0 }
要求结果是: { "MarketCode" = "总计", "MarketName" = "总计", "Site" ="" , "ResType" = "{ ResType = BCC }", "FrozenAmount" = 0, "FreeAmount" = 0.0039963, "LocalValue" = 0, "ReferValue" = 0 } 展开
1个回答
展开全部
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
string str="{ MarketCode = 总计, MarketName = 总计, Site = , ResType = { ResType = BCC }, FrozenAmount = 0, FreeAmount = 0.0039963, LocalValue = 0, ReferValue = 0 }";
Regex reg1 = new Regex(@"([^=\s\{\[,]+)([=\s]+)([^=\s\}\],]*)");
Regex reg2 = new Regex(@"\=\s*([^=\s\}\],\{\[\d]*)\s*(?=[,\}\]])");
str = reg1.Replace(str,"\"$1\"$2$3");
str = reg2.Replace(str,"=\"$1\"");
Console.WriteLine(str);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询