0

So, I'm working on a school project, and I'm trying to figure out the best way to deal with this data file that contains a pretty large amount of JSON objects. I know the basics of VB.net, basic event handling, etc.

I know the basics of designing Structures, and stuff like that, but I need to figure out how to parse and create a list of objects from a 5MB JSON file that contains entries such as the following:

{
    "Air Elemental":{
        "layout":"normal",
        "name":"Air Elemental",
        "manaCost":"{3}{U}{U}",
        "cmc":5,
        "colors":[
            "Blue"
        ],
        "type":"Creature — Elemental",
        "types":[
            "Creature"
        ],
        "subtypes":[
            "Elemental"
        ],
        "text":"Flying",
        "power":"4",
        "toughness":"4",
        "imageName":"air elemental"
    },
    "Ancestral Recall":{
        "layout":"normal",
        "name":"Ancestral Recall",
        "manaCost":"{U}",
        "cmc":1,
        "colors":[
            "Blue"
        ],
        "type":"Instant",
        "types":[
            "Instant"
        ],
        "text":"Target player draws three cards.",
        "imageName":"ancestral recall"
    },
    "Animate Artifact":{
        "layout":"normal",
        "name":"Animate Artifact",
        "manaCost":"{3}{U}",
        "cmc":4,
        "colors":[
            "Blue"
        ],
        "type":"Enchantment — Aura",
        "types":[
            "Enchantment"
        ],
        "subtypes":[
            "Aura"
        ],
        "text":"Enchant artifact\nAs long as enchanted artifact isn't a creature, it's an artifact creature with power and toughness each equal to its converted mana cost.",
        "imageName":"animate artifact"
    }
}

If anyone could be of assistance, or just sort of point me in the right direction, I'd really appreciate it. I think the part that's throwing me off the most is that each card name is a key in itself, and all of the card's data is the value associated with the name "key"...

2 Answers 2

2

In this case it hardly matters how many there are because they are all structured the same way:

Public Class Spell
    Public Property layout As String
    Public Property name As String
    Public Property manaCost As String
    Public Property cmc As Integer
    Public Property colors As String()
    Public Property type As String
    Public Property types As String()
    Public Property subtypes As String()
    Public Property text As String
    Public Property power As String
    Public Property toughness As String
    Public Property imageName As String
End Class

I have no idea what the data represents, it looks like some fantasy game. When deserialized (I used Newtonsoft), you will end up with a Dictionary of spells with the key being "Air Elemental" and "Animate Artifact" etc. It takes but one line of code:

Dim jstr As String = from whereever   
Dim mySpells = JsonConvert.DeserializeObject(Of Dictionary(Of String, Spell))(jstr)

Using the NET JavaScriptSerializer is about the same:

Dim jss As New JavaScriptSerializer
Dim myspells = jss.DeserializeObject(jstr)

You can use http://jsonutils.com/, or even Paste Special in VS, to help parse what the structures (classes, really) need to look like. However, it pays to use your brain and look at them. A robot will create 3 classes for that JSON and another class container to hold them. If there were 100 of them it would be needlessly long.

Since each item is identical and the JSON one class can be used for each. Since the JSON is formatted to accommodate, a standard NET dictionary works fine.

Sign up to request clarification or add additional context in comments.

6 Comments

Cool, giving it a shot now.
So, I tried to get say, a length of the dictionary of cards that was generated from reading this file I tried to parse, and it doesn't seem to be working. Code is here: pastebin.com/5EWVKTVL
you cannot deserialize a line at a time. get rid of streamreader and just use jstr = File.ReadAllText(datafile)
Done. Still isn't returning any count (I'd assume it would be giving me a length or something once it's deserialized...) --> pastebin.com/f18FTxKd
are you trying to work on a larger, full size file? That Card class only covers what you posted. If there is other data in there it might not match and so would throw an exception. I also would specify a path for the file such as "C:\Temp" so you know which version and from where it is loading. I'd also do it in a button click rather than formload
|
1

There is a wonderful feature in VS 2013 called "Paste as JSON" under the Edit menu, so copy your JSON string and choose this feature.

Be aware that there is a small bug that I reported to MS in the way that it declares arrays. The text it gives you will be

Public Property x() as DataType

however it needs to be changed to

Public Property x as DataType()

in order to be correctly declared as an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.