转换查询字符串字典(或关联数组)字典 [英] Convert query string dictionary (or associative array) to dictionary
问题描述
我试图接受以下形式的查询字符串
参数[键1] =值1&放大器;参数[键2] = VALUE2
并将其转换成一个字典在C#MVC 4。这是微不足道的在PHP做的,但我一直没能找到在C#复制本的任何方法。
我知道我可以通过
取一个数组 参数=值&放大器;参数=值
但还有排序问题,是几乎没有,我的目的是有用的。
在希望,这不是框架的限制,我如何实现在C#字典风格转换?
要澄清:我不是找一个查询字符串转换为雷士,而是查询字符串参数转换为自己的NVCS。这是的不的那样简单ParseQueryString()或的Request.QueryString对象。
块引用>解决方案在这里我固定的一些code,它会为你工作,杰福不是最好的solutioin但它应该工作。
字符串即basestring =参数[键1] =值1&放大器;参数[键2] =值;
串newstring = basestring.Replace(?,).Replace(参数[,).Replace(],);
VAR阵列= newstring.Split('和;');
VAR词典=新词典<字符串,字符串>();
的foreach(数组VAR onestring)
{
VAR splitedstr = onestring.Split('=');
dictionary.Add(splitedstr [0],splitedstr [1]);
}I'm trying to accept a query string of the form
?param[key1]=value1¶m[key2]=value2
and convert it into a Dictionary in C# MVC 4. This is trivial to do in PHP, but I haven't been able to find any method of reproducing this in C#.
I know I can take an array via
?param=value1¶m=value2
but that leaves issues of ordering and isn't nearly as useful for my purposes.
In the hopes that this isn't a limitation of the framework, how might I implement a dictionary-style conversion in C#?
To clarify: I'm not looking to convert a query string into an NVC, but rather to convert query string parameters into their own NVCs. This is NOT as simple as ParseQueryString() or the Request.QueryString object.
解决方案here I fixed some code that will work for you, defo not the best solutioin but it should work.
string basestring = "?param[key1]=value1¶m[key2]=value2"; string newstring = basestring.Replace("?", "").Replace("param[", "").Replace("]", ""); var array = newstring.Split('&'); var dictionary = new Dictionary<string, string>(); foreach (var onestring in array) { var splitedstr = onestring.Split('='); dictionary.Add(splitedstr[0],splitedstr[1]); }
这篇关于转换查询字符串字典(或关联数组)字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!