3

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.

user1477388
  • 20,790
  • 32
  • 144
  • 264

2 Answers2

3

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

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
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thanks, it's strange because I'm sending it via jQuery AJAX with "type" set as "json." – user1477388 Oct 08 '12 at 12:39
  • 1
    Although this answers the OP's question, it's not the recommended approach to what it is they are trying to do. With MVC it's better to introduce a view model and let the server bind the model for you. @user1477388 setting the `contentType` of the parameters doesn't mean it will be parsed as JSON, you are simply informing the server of what type of data to expect, its still up to the server to handle the binding. With MVC it will automatically bind key/value & JSON data to a model matching the same properties. – James Oct 08 '12 at 13:02
  • 1
    @user1477388 have a read of [The Features and Foibles of ASP.NET MVC Model Binding](http://msdn.microsoft.com/en-us/magazine/hh781022.aspx) – James Oct 08 '12 at 13:16
3

Well firstly, your error occurs because your attempting to parse a key/value pair string as a JSON object (which it obviously isn't). Secondly, your using MVC, there should be no need for any manual serialization server side, let the ASP.NET MVC model binder do that for you. Introduce a view model for your action e.g.

Public Class QuizScoreViewModel

    Property Property1 As String
    Property Property2 As String
    ...

End Class

Then update your action parameter to expect QuizScoreViewModel e.g.

<HttpPost()>
Function GetQuizScore(ByVal viewModel As QuizScoreViewModel) As JsonResult

    Debug.Print(viewModel.Property1)
    ...

End Function
James
  • 80,725
  • 18
  • 167
  • 237
  • Thanks, James. That's an interesting idea. – user1477388 Oct 08 '12 at 12:51
  • 1
    @user1477388 when using MVC it's the correct way of doing things. – James Oct 08 '12 at 12:54
  • 1
    You can shorten your code a bit by using [automatic properties](http://msdn.microsoft.com/en-us/library/dd293589.aspx), e.g. `Public Property Property1 As String`. Oh, any +1, by the way, for answering with what the OP needs instead of only what the OP asked. ;-) – Heinzi Oct 08 '12 at 13:33
  • @Heinzi thanks for that, I have updated my answer. Just goes to show how long it's been since I have written VB.NET because it didn't support auto-implemented props! – James Oct 08 '12 at 13:38