简体   繁体   English

如何将查询字符串转换为 json 字符串?

[英]How do I convert a querystring to a json string?

Using server-side C#, how can I convert a querystring to a JSON string of keys and values?使用服务器端 C#,如何将查询字符串转换为键和值的 JSON 字符串? For example, I want to convert例如,我想转换

"ID=951357852456&FNAME=Jaime&LNAME=Lopez"

to

{ "ID":"951357852456" , "FNAME":"Jaime" , "LNAME":"Lopez" }

I know how to manually parse and format but, before starting down that road, I thought I'd ask since there might be a library that does it better.我知道如何手动解析和格式化,但在开始这条路之前,我想我会问,因为可能有一个库做得更好。 Thanks!谢谢!

This gives the exactly same json you want这给出了你想要的完全相同的 json

var dict = HttpUtility.ParseQueryString("ID=951357852456&FNAME=Jaime&LNAME=Lopez");
var json = new JavaScriptSerializer().Serialize(
                    dict.AllKeys.ToDictionary(k => k, k => dict[k])
           );

It also possible to use也可以使用

var collection = HttpUtility.ParseQueryString(query);
Newtonsoft.Json.JsonConvert.SerializeObject(collection.AllKeys.ToDictionary(y => y, y => collection[y]));

您可以使用 jQuery 来执行此操作: jQuery.Param

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM