C# 将json字符串转换为键值对形式,用于执行form表单形式的请求
程序员文章站
2022-06-12 19:59:48
...
需要添加引用:
System.Web
System.Web.Extensions
需要引入命名空间:
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Serialization;
string KeyValueCombination(string str)
{
string paraUrlCoded = "";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str);
foreach (var j in json)
{
paraUrlCoded += System.Web.HttpUtility.UrlEncode(j.Key);
if (j.Value.GetType() == typeof(object[]))
{
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jss.Serialize(j.Value)) + "&";
}
else
{
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(j.Value.ToString()) + "&";
}
}
if (!string.IsNullOrWhiteSpace(paraUrlCoded))
{
paraUrlCoded = paraUrlCoded.Substring(0, paraUrlCoded.Length - 1);
}
return paraUrlCoded;
}
上一篇: 小程序避免一分钟内点击多次
下一篇: 被隐藏了的jni