Reputation: 1502
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim json = js.Deserialize(Of jsonTest)(e.Result)
With json
'do stuff with object
End With
Private Class jsonTest
Public Artist As String
Public Album As String
Public Track As String
Public Duration As String
End Class
I managed to get this to work. However, I can't get it to work for multiple results.
Here's an example of my JSON output. How would I return multiple sets? "album" : "Move Along", "albumpos" : 1, "artist" : "The All American Rejects", "bitrate" : 128, "duration" : 195, "playing" : true, "position" : 101, "resolvedBy" : "DatabaseResolver", "score" : 1.0, "track" : "Track 01"
Upvotes: 0
Views: 618
Reputation: 3030
Ok, "multiple results" can mean a bunch of things. That's why nobody has given you a straight answer yet.
I THINK - you are asking about the idea of collections and sets on the server side. In other words, how do I return a json result set that looks like this (a json array):
[
{"Artist": "AMR", Album:"I Have No Idea", "Track": "Track 01", Duration: 195},
{"Artist": "AMR", Album:"I Have No Idea", "Track": "Track 02", Duration: 500},
{"Artist": "AMR", Album:"I Have No Idea", "Track": "Track 03", Duration: 400},
...
]
Then, If I'd REALLY like to return these various tracks grouped by album (or search phrase or some other arbitrary group), how do I return a result set that looks like this (an array of objects):
[
{"The All-American Rejects": [{"Artist":"AMR", ...}, {Artist:"AMR", ...}]}
{"Move Along": [{"Artist":"AMR", ...}, {Artist:"AMR", ...}]}
{"When the Work Comes Down": [{"Artist":"AMR", ...}, {Artist:"AMR", ...}]}
...
]
If that's NOT the intent of your question, then stop reading here. If it is, then you are looking for 2 objects in your VB code: a LIST and a DICTIONARY. Your class will hold the data for one and only one track. LIST and DICTIONARY are both standard collections within the language (a smarter sexier version of an array).
Let's rearrange, rename some of your code so that we use a List (and some names that make sense)...
Private Class Track
Public Property Artist As String
Public Property Album As String
Public Property Track As String
Public Property Duration As String
End Class
'CREATE MY LIST OF OBJECTS HERE
dim Album1 as new List(of Track)
Album1.Add(New Track() with {
.Artist = "AMR",
.Album = "The All American Rejects",
.Track = "Track 01",
.Duration = 910
})
Album1.Add(New Track() with {
... add track info again...
})
... add yet more track info ...
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim json = js.Deserialize(Of List(of Track))(Album1)
... do whatever you'd like next ...
See how we messed with your Deserialize code?
js.Deserialize(Of List(of Track))(Album1)
Now, instead of deserializing 1 result, we are deserializing a LIST OF results. That code will give you the first result set (more or less). OK. Cool. So how do we return multiple albums? For that little trick, use a dictionary.
To create a list of tracks, the vb.net code is similar, but we use a dictionary AND a list:
'CREATE MY LIST OF OBJECTS HERE
dim results as new Dictionary(of string, of List(of Track))
dim Album1 as new List(of Track)
Album1.Add(New Track() with {
.Artist = "AMR",
.Album = "The All American Rejects",
.Track = "Track 01",
.Duration = 910
})
Album1.Add(New Track() with {
... add track info again...
})
... add yet more track info ...
results.Add("The All American Rejects", Album1)
... do the same thing with the next album ...
results.Add("MoveAlong", Album2)
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim json = js.Deserialize(Of Dictionary(of string, List(of Track)))(results)
... do whatever you'd like next ...
That code will return that second set of json data that we outlined, like, 10,000 words ago.
IF this is the direction you wanted to head, then take a look at all the collections available at the server level, and maybe take a look at how to create properties from those collections. Those objects are where most of your "real" server code is going to live. And, if you code them correctly, serializing them (to json, to the database, to XML, etc) will be almost trivial!
Start by building on your Track class. Create an album class that looks like the one below and then populate it, serialize it and see what the result set looks like:
Public class Album
Public Property Name as String,
Public Property ReleaseDt as DateTime,
Public Property Tracks as List(of Track) = New List(of Track)
End Class
Hope that's what you were looking for. If it is, then stop right here. Go no further. Take one half-step backward and read a decent intro to Design Patterns. It'll get you used to working with Interfaces and objects and inheritance. And THAT will make any server-side code you write stunningly beautiful. Like ... drop dead sexy. Trust me. It's worth the time.
Good luck with the project.
Upvotes: 1