51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

C# 将json字符串转换为键值对形式,用于执行form表单形式的请求


需要添加引用:

  • 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;
        }</code></pre>



 



 
​

赞(0)
未经允许不得转载:工具盒子 » C# 将json字符串转换为键值对形式,用于执行form表单形式的请求