1

How can I convert a query string to a JSON string of keys and values? For example, I want to convert

"ID=123&FNAME=test&LNAME=xyz"

to

{"ID":"123","FNAME":"test","LNAME":"xys"}
1
  • 2
    What have you tried and what is not working for you? Commented Dec 1, 2021 at 18:06

1 Answer 1

3

If you are using asp.net core, I suggest you could use System.Text.Json.JsonSerializer class to achieve your requirement.

More details, you could refer to below codes:

        var dict = HttpUtility.ParseQueryString("ID=123&FNAME=test&LNAME=xyz");
        var json = System.Text.Json.JsonSerializer.Serialize(
                            dict.AllKeys.ToDictionary(k => k, k => dict[k])
                   );

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.