url参数转json,先转为键值对,再转json 需要用到Newtonsoft.Json
public string ConvertJson(string str)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
MatchCollection mc = re.Matches(str);
foreach (Match m in mc)
{
dic.Add(m.Result("$2"), m.Result("$3"));
}
string json = JsonConvert.SerializeObject(dic);
return json;
}
有url时,url中的&符号需要替换下,不然会出问题
本文介绍了一种将URL中的参数转换成JSON格式的方法。通过使用正则表达式解析URL参数并将其存储到字典中,最后利用Newtonsoft.Json库将字典序列化为JSON字符串。
996

被折叠的 条评论
为什么被折叠?



