Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Parse Query String to Array

I have this string:

1=True&2=150+minutes&3=True&4=True&5=Continuing+to+smoke

How can I get it into an array or object like this:

[1] => True
[2] => "150 minutes"
etc?

I have tried this:

<HttpPost()>
Function GetQuizScore(ByVal data As String) As JsonResult

    'Debug.Print(data)

    Dim jss = New JavaScriptSerializer

    Dim dict = jss.Deserialize(Of List(Of String))(data)

    Debug.Print(String.Join(", ", dict))

    Return Json(data)

End Function

But, it gives me an error that says:

Invalid JSON primitive: True&2=150+minutes&3=True&4=True&5=Continuing+to+smoke.

Thanks for your help.

like image 408
user1477388 Avatar asked Feb 19 '26 21:02

user1477388


1 Answers

The string you have is a QueryString, not a JSON string. Thus, you can use

  • HttpUtility.ParseQueryString

to convert it into a NameValueCollection.


Example:

Dim s = "1=True&2=150+minutes&3=True&4=True&5=Continuing+to+smoke"

Dim parsed = HttpUtility.ParseQueryString(s)

For Each key In parsed
    Console.WriteLine(key & ": " & parsed(key))
Next

Output:

1: True
2: 150 minutes
3: True
4: True
5: Continuing to smoke
like image 89
Heinzi Avatar answered Feb 21 '26 13:02

Heinzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!