private void btnBegin_Click(object sender, EventArgs e)
{
string json = txtJson.Text.Trim();
//1.赋值
json = json.Replace(":", "=");
//2.属性名转换
json = Regex.Replace(json, @"""([^""]+)""=", "$1=");
//3.属性值转换
json = Regex.Replace(json, @"([=,\[])""[^""]+""", @"$1"""""); //字符串
json = Regex.Replace(json, @"([=,\[])\-?\d*(\d)(\.\d)?\d*", "$1$2$3"); //数值
//4.数组保留一个项(可选)
json = Regex.Replace(json, @"(,"""")+", "");
json = Regex.Replace(json, @"(,\d(\.\d)?)+", "");
json = Regex.Replace(json, @",\{[^\}]+\}", ""); //复杂对象不适用
json = Regex.Replace(json, @",\[[^\]]+\]", ""); //复杂数组不适用
//5.对象、数组转换
json = json.Replace("{", "new {");
json = json.Replace("]", "}");
json = json.Replace("[", "new[] {");
txtAnonymousTypeObject.Text = json;
}
|