-1

I am making an application in vb net, and read JSON format files. I know how, but I have a file that has a different format, you can see here:

{
  "objects": {
    "realms/lang/de_DE.lang": {
      "hash": "729b2c09d5c588787b23127eeda2730f9c039194",
      "size": 7784
    },
    "realms/lang/cy_GB.lang": {
      "hash": "7b52463b2df4685d2d82c5d257fd5ec79843d618",
      "size": 7688
    },
    "minecraft/sounds/mob/blaze/breathe4.ogg": {
      "hash": "78d544a240d627005aaef6033fd646eafc66fe7a",
      "size": 22054
    },
    "minecraft/sounds/dig/sand4.ogg": {
      "hash": "37afa06f97d58767a1cd1382386db878be1532dd",
      "size": 5491
    }
  }
}

It is different because all text has objects, no string, so I can not read it defined classes. I just need the values of "hash", then add them to a textbox. I hope you can help me.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Heber
  • 3
  • 1
  • 3

2 Answers2

1

Sounds like @Plutonix has seen this before, so I'll go with the Minecraft thing. It seems like objects is a Dictionary(Of String, MinecraftItem). A little tweak to the linked duplicate could be to create a class for the whole file, so you don't have to pull out the objects separately:

Public Class MinecraftData
    Public Property Objects As Dictionary(Of String, MinecraftItem)
End Class

Public Class MinecraftItem
    Public Property Hash As String
    Public Property Size As Long
End Class

You can then parse the whole thing using Json.NET:

Dim data = JsonConvert.DeserializeObject(Of MinecraftData)(json)
Mark
  • 8,140
  • 1
  • 14
  • 29
0

If you are certain that you will only ever need the hashes, you can actually use a regex to get all the hashes from this file. Yes, this is slightly evil, because regex is normally not a suitable tool for dealing with structured data. But if you don't care about the structure...

Dim hashExtractor = new System.Text.RegularExpressions.Regex("[0-9a-f]{40}")
Dim matches = hashExtractor.Matches(myFileContents).Cast(of System.Text.RegularExpressions.Match)
Dim hashes = matches.Select(Function(m) m.Value).ToList()
Jerry Federspiel
  • 1,504
  • 10
  • 14