Using server-side C#, how can I convert a querystring to a JSON string of keys and values? 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!
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
This gives the exactly same json you want
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]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With