user3851593 user3851593 - 9 months ago 21 Question

Thread Options How would I do this with JSON

Sorry for the noob questions. I'm trying to make an in-house application so nobody else will be using it.

Basically I have a JSON here.

"resources": [
"spritemap": {
"click": {
"start": 0,
"end": 0.23034013605442177,
"loop": false
"CoinCMixed": {
"start": 2,
"end": 2.222448979591837,
"loop": false
"CoinDropMixed": {
"start": 4,
"end": 4.312222222222222,
"loop": false

I need to get the Name, Which would be click, The start and End.

The problem is, The name isn't always the same thing. It's unique. So, I'm guessing what I need to do is loop through spritemap? But I don't know how to do that.

So for example.

  • Name: CoinDropMixed

  • start: 4

  • end: 4.3122222

Which would allow me to do

msgbox("Your song name is " + Name + "The length is " + start + "The end is" + end)


You can handle the varying names by using a Dictionary. Define your classes like this:

Class DataObject
    Public Property resources As List(Of String)
    Public Property spritemap As Dictionary(Of String, Sound)
End Class

Class Sound
    Public Property start As Double
    Public Property [end] As Double
    Public Property [loop] As Boolean
End Class

Then you can deserilalize the JSON like this:

Dim data As DataObject = JsonConvert.DeserializeObject(Of DataObject)(json)

You can then loop over the dictionary like this to get the data you want:

For Each kvp As KeyValuePair(Of String, Sound) In data.spritemap
    Console.WriteLine("Name: " + kvp.Key)
    Console.WriteLine("Start: " + kvp.Value.start.ToString())
    Console.WriteLine("End: " + kvp.Value.end.ToString())