Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

vb.net - vb .net iterate dynamic json

I have dynamic jSON that could contain keys and subkeys. Since each jSON could have more or less keys/subkeys (some of they may or not appear), I'd like to iterate the Object and get all information (key and value). My problem is this iteration since I don't see how can I achieve this.

Dim JSON_Obj1 As Object = New JavaScriptSerializer().DeserializeObject(JSON_Response)

When trying to access data:

        For Each j In JSON_Obj1
            Console.WriteLine(j.ToString)
        Next

This code outputs the data like: If it's a key:

[id, 130ee895-1e5e-4f82-bb66-e1275bac75dd]

if it's a subkey:

[image_uris, System.Collections.Generic.Dictionary`2[System.String,System.Object]]

After checking my j var, I don't have many options that could get that information. How can I achieve this automatically, without need to make checks if keys exists and retrieve their value and key name?

Edit: Added 2 jSON example

{
  "object": "card",
  "id": "3606519e-5677-4c21-a34e-be195b6669fa",
  "oracle_id": "35ae7b25-6d0f-49f8-a40d-dd2cf88e6f44",
  "multiverse_ids": [
    503627,
    503626
  ],
  "mtgo_id": 87365,
  "arena_id": 75057,
  "tcgplayer_id": 230921,
  "cardmarket_id": 532112,
  "name": "Reidane, God of the Worthy // Valkmira, Protector's Shield",
  "lang": "en",
  "released_at": "2021-02-05",
  "uri": "https://api.scryfall.com/cards/3606519e-5677-4c21-a34e-be195b6669fa",
  "scryfall_uri": "https://scryfall.com/card/khm/21/reidane-god-of-the-worthy-valkmira-protectors-shield?utm_source=api",
  "layout": "modal_dfc",
  "highres_image": false,
  "cmc": 3.0,
  "type_line": "Legendary Creature — God // Legendary Artifact",
  "color_identity": [
    "W"
  ],
  "keywords": [
    "Flying",
    "Vigilance"
  ],
  "card_faces": [
    {
      "object": "card_face",
      "name": "Reidane, God of the Worthy",
      "mana_cost": "{2}{W}",
      "type_line": "Legendary Creature — God",
      "oracle_text": "Flying, vigilance
Snow lands your opponents control enter the battlefield tapped.
Noncreature spells your opponents cast with converted mana cost 4 or greater cost {2} more to cast.",
      "colors": [
        "W"
      ],
      "power": "2",
      "toughness": "3",
      "artist": "Jason Rainville",
      "artist_id": "6ed7e669-579b-443d-b223-e5cbcb2a7483",
      "illustration_id": "50842425-89ab-475d-afce-b43b28f9aa43",
      "image_uris": {
        "small": "https://c1.scryfall.com/file/scryfall-cards/small/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "normal": "https://c1.scryfall.com/file/scryfall-cards/normal/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "large": "https://c1.scryfall.com/file/scryfall-cards/large/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "png": "https://c1.scryfall.com/file/scryfall-cards/png/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.png?1611000242",
        "art_crop": "https://c1.scryfall.com/file/scryfall-cards/art_crop/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "border_crop": "https://c1.scryfall.com/file/scryfall-cards/border_crop/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242"
      }
    },
    {
      "object": "card_face",
      "name": "Valkmira, Protector's Shield",
      "mana_cost": "{3}{W}",
      "type_line": "Legendary Artifact",
      "oracle_text": "If a source an opponent controls would deal damage to you or a permanent you control, prevent 1 of that damage.
Whenever you or a permanent you control becomes the target of a spell or ability an opponent controls, counter that spell or ability unless its controller pays {1}.",
      "colors": [
        "W"
      ],
      "artist": "Jason Rainville",
      "artist_id": "6ed7e669-579b-443d-b223-e5cbcb2a7483",
      "illustration_id": "d8a037ea-c67b-4290-81ef-cdb8073c46cb",
      "image_uris": {
        "small": "https://c1.scryfall.com/file/scryfall-cards/small/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "normal": "https://c1.scryfall.com/file/scryfall-cards/normal/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "large": "https://c1.scryfall.com/file/scryfall-cards/large/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "png": "https://c1.scryfall.com/file/scryfall-cards/png/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.png?1611000242",
        "art_crop": "https://c1.scryfall.com/file/scryfall-cards/art_crop/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242",
        "border_crop": "https://c1.scryfall.com/file/scryfall-cards/border_crop/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242"
      }
    }
  ],
  "legalities": {
    "standard": "legal",
    "future": "legal",
    "historic": "legal",
    "gladiator": "legal",
    "pioneer": "legal",
    "modern": "legal",
    "legacy": "legal",
    "pauper": "not_legal",
    "vintage": "legal",
    "penny": "not_legal",
    "commander": "legal",
    "brawl": "legal",
    "duel": "legal",
    "oldschool": "not_legal",
    "premodern": "not_legal"
  },
  "games": [
    "arena",
    "paper",
    "mtgo"
  ],
  "reserved": false,
  "foil": true,
  "nonfoil": true,
  "oversized": false,
  "promo": false,
  "reprint": false,
  "variation": false,
  "set": "khm",
  "set_name": "Kaldheim",
  "set_type": "expansion",
  "set_uri": "https://api.scryfall.com/sets/43057fad-b1c1-437f-bc48-0045bce6d8c9",
  "set_search_uri": "https://api.scryfall.com/cards/search?order=set&q=e%3Akhm&unique=prints",
  "scryfall_set_uri": "https://scryfall.com/sets/khm?utm_source=api",
  "rulings_uri": "https://api.scryfall.com/cards/3606519e-5677-4c21-a34e-be195b6669fa/rulings",
  "prints_search_uri": "https://api.scryfall.com/cards/search?order=released&q=oracleid%3A35ae7b25-6d0f-49f8-a40d-dd2cf88e6f44&unique=prints",
  "collector_number": "21",
  "digital": false,
  "rarity": "rare",
  "card_back_id": "0aeebaf5-8c7d-4636-9e82-8c27447861f7",
  "artist": "Jason Rainville",
  "artist_ids": [
    "6ed7e669-579b-443d-b223-e5cbcb2a7483"
  ],
  "border_color": "black",
  "frame": "2015",
  "frame_effects": [
    "legendary"
  ],
  "full_art": false,
  "textless": false,
  "booster": true,
  "story_spotlight": false,
  "edhrec_rank": 15706,
  "preview": {
    "source": "Marty Friedman",
    "source_uri": "https://twitter.com/marty_friedman/status/1349527247560994816",
    "previewed_at": "2021-01-13"
  },
  "prices": {
    "usd": "0.87",
    "usd_foil": "5.68",
    "eur": "1.37",
    "eur_foil": "2.00",
    "tix": null
  },
  "related_uris": {
    "gatherer": "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=503627",
    "tcgplayer_decks": "https://decks.tcgplayer.com/magic/deck/search?contains=Reidane%2C+God+of+the+Worthy&page=1&utm_campaign=affiliate&utm_medium=api&utm_source=scryfall",
    "edhrec": "https://edhrec.com/route/?cc=Reidane%2C+God+of+the+Worthy",
    "mtgtop8": "https://mtgtop8.com/search?MD_check=1&SB_check=1&cards=Reidane%2C+God+of+the+Worthy"
  },
  "purchase_uris": {
    "tcgplayer": "https://shop.tcgplayer.com/product/productsearch?id=230921&utm_campaign=affiliate&utm_medium=api&utm_source=scryfall",
    "cardmarket": "https://www.cardmarket.com/en/Magic/Products/Singles/Kaldheim/Reidane-God-of-the-Worthy-Valkmira-Protectors-Shield?referrer=scryfall&utm_campaign=card_prices&utm_medium=text&utm_source=scryfall",
    "cardhoarder": "https://www.cardhoarder.com/cards/87365?affiliate_id=scryfall&ref=card-profile&utm_campaign=affiliate&utm_medium=card&utm_source=scryfall"
  }
}

Example 2:

{
  "object": "card",
  "id": "03595195-3be2-4d18-b5c0-43b2dcc1c0f5",
  "oracle_id": "eeb07c8a-21d1-43fa-a5f3-ee3fe328a671",
  "multiverse_ids": [
    503625
  ],
  "mtgo_id": 87361,
  "arena_id": 75056,
  "tcgplayer_id": 230200,
  "cardmarket_id": 530517,
  "name": "Rally the Ranks",
  "lang": "en",
  "released_at": "2021-02-05",
  "uri": "https://api.scryfall.com/cards/03595195-3be2-4d18-b5c0-43b2dcc1c0f5",
  "scryfall_uri": "https://scryfall.com/card/khm/20/rally-the-ranks?utm_source=api",
  "layout": "normal",
  "highres_image": false,
  "image_uris": {
    "small": "https://c1.scryfall.com/file/scryfall-cards/small/front/0/3/03595195-3be2-4d18-b5c0-43b2dcc1c0f5.jpg?1610585956",
    "normal": "https://c1.scryfall.com/file/scryfall-cards/normal/front/0/3/03595195-3be2-4d18-b5c0-43b2dcc1c0f5.jpg?1610585956",
    "large": "https://c1.scryfall.com/file/scryfall-cards/large/front/0/3/03595195-3be2-4d18-b5c0-43b2dcc1c0f5.jpg?1610585956",
    "png": "https://c1.scryfall.com/file/scryfall-cards/png/front/0/3/03595195-3be2-4d18-b5c0-43b2dcc1c0f5.png?1610585956",
    "art_crop": "https://c1.scryfall.com/file/scryfall-cards/art_crop/front/0/3/03595195-3be2-4d18-b5c0-43b2dcc1c0f5.jpg?1610585956",
    "border_crop": "https://c1.scryfall.com/file/scryfall-cards/border_crop/front/0/3/03595195-3be2-4d18-b5c0-43b2dcc1c0f5.jpg?1610585956"
  },
  "mana_cost": "{1}{W}",
  "cmc": 2.0,
  "type_line": "Enchantment",
 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

OK, so you're looking to flatten your JSON.

Private Shared Sub FlattenInto(ByVal o As Object, ByVal into As StringBuilder)
    If TypeOf o Is JObject
        For Each t As KeyValuePair(Of String, JToken) In CType(o, JObject)
            FlattenInto(t.Value, into)
        Next
    Else If TypeOf o Is JArray
        For Each e In CType(o, JArray)
            FlattenInto(e, into)
        Next
    Else If TypeOf o is JValue
        Dim jValue = CType(o, JValue)
        into.AppendLine($"{jValue.Path}:{jValue.Value}")
    Else
        Throw New Exception($"Unsupported type: {o.[GetType]()}")
    End If
End Sub


Dim d = Newtonsoft.Json.JsonConvert.DeserializeObject(YOUR_JSON)

Dim sb = new StringBuilder()
FlattenInto(d, sb)

sb now contains like:

object:card
id:3606519e-5677-4c21-a34e-be195b6669fa
oracle_id:35ae7b25-6d0f-49f8-a40d-dd2cf88e6f44
multiverse_ids[0]:503627
multiverse_ids[1]:503626
mtgo_id:87365
arena_id:75057
tcgplayer_id:230921
cardmarket_id:532112
name:Reidane, God of the Worthy // Valkmira, Protector's Shield
lang:en
released_at:2021-02-05
uri:https://api.scryfall.com/cards/3606519e-5677-4c21-a34e-be195b6669fa
scryfall_uri:https://scryfall.com/card/khm/21/reidane-god-of-the-worthy-valkmira-protectors-shield?utm_source=api
layout:modal_dfc
highres_image:False
cmc:3
type_line:Legendary Creature — God // Legendary Artifact
color_identity[0]:W
keywords[0]:Flying
keywords[1]:Vigilance
card_faces[0].object:card_face
card_faces[0].name:Reidane, God of the Worthy
card_faces[0].mana_cost:{2}{W}
card_faces[0].type_line:Legendary Creature — God
card_faces[0].oracle_text:Flying, vigilance
Snow lands your opponents control enter the battlefield tapped.
Noncreature spells your opponents cast with converted mana cost 4 or greater cost {2} more to cast.
card_faces[0].colors[0]:W
card_faces[0].power:2
card_faces[0].toughness:3
card_faces[0].artist:Jason Rainville
card_faces[0].artist_id:6ed7e669-579b-443d-b223-e5cbcb2a7483
card_faces[0].illustration_id:50842425-89ab-475d-afce-b43b28f9aa43
card_faces[0].image_uris.small:https://c1.scryfall.com/file/scryfall-cards/small/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[0].image_uris.normal:https://c1.scryfall.com/file/scryfall-cards/normal/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[0].image_uris.large:https://c1.scryfall.com/file/scryfall-cards/large/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[0].image_uris.png:https://c1.scryfall.com/file/scryfall-cards/png/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.png?1611000242
card_faces[0].image_uris.art_crop:https://c1.scryfall.com/file/scryfall-cards/art_crop/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[0].image_uris.border_crop:https://c1.scryfall.com/file/scryfall-cards/border_crop/front/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[1].object:card_face
card_faces[1].name:Valkmira, Protector's Shield
card_faces[1].mana_cost:{3}{W}
card_faces[1].type_line:Legendary Artifact
card_faces[1].oracle_text:If a source an opponent controls would deal damage to you or a permanent you control, prevent 1 of that damage.
Whenever you or a permanent you control becomes the target of a spell or ability an opponent controls, counter that spell or ability unless its controller pays {1}.
card_faces[1].colors[0]:W
card_faces[1].artist:Jason Rainville
card_faces[1].artist_id:6ed7e669-579b-443d-b223-e5cbcb2a7483
card_faces[1].illustration_id:d8a037ea-c67b-4290-81ef-cdb8073c46cb
card_faces[1].image_uris.small:https://c1.scryfall.com/file/scryfall-cards/small/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[1].image_uris.normal:https://c1.scryfall.com/file/scryfall-cards/normal/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[1].image_uris.large:https://c1.scryfall.com/file/scryfall-cards/large/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[1].image_uris.png:https://c1.scryfall.com/file/scryfall-cards/png/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.png?1611000242
card_faces[1].image_uris.art_crop:https://c1.scryfall.com/file/scryfall-cards/art_crop/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
card_faces[1].image_uris.border_crop:https://c1.scryfall.com/file/scryfall-cards/border_crop/back/3/6/3606519e-5677-4c21-a34e-be195b6669fa.jpg?1611000242
legalities.standard:legal
legalities.future:legal
legalities.historic:legal
legalities.gladiator:legal
legalities.pioneer:legal
legalities.modern:legal
legalities.legacy:legal
legalities.pauper:not_legal
legalities.vintage:legal
legalities.penny:not_legal
legalities.commander:legal
legalities.brawl:legal
legalities.duel:legal
legalities.oldschool:not_legal
legalities.premodern:not_legal
games[0]:arena
games[1]:paper
games[2]:mtgo
reserved:False
foil:True
nonfoil:True
oversized:False
promo:False
reprint:False
variation:False
set:khm
set_name:Kaldheim
set_type:expansion
set_uri:https://api.scryfall.com/sets/43057fad-b1c1-437f-bc48-0045bce6d8c9
set_search_uri:https://api.scryfall.com/cards/search?order=set&q=e%3Akhm&unique=prints
scryfall_set_uri:https://scryfall.com/sets/khm?utm_source=api
rulings_uri:https://api.scryfall.com/cards/3606519e-5677-4c21-a34e-be195b6669fa/rulings
prints_search_uri:https://api.scryfall.com/cards/search?order=released&q=oracleid%3A35ae7b25-6d0f-49f8-a40d-dd2cf88e6f44&unique=prints
collector_number:21
digital:False
rarity:rare
card_back_id:0aeebaf5-8c7d-4636-9e82-8c27447861f7
artist:Jason Rainville
artist_ids[0]:6ed7e669-579b-443d-b223-e5cbcb2a7483
border_color:black
frame:2015
frame_effects[0]:legendary
full_art:False
textless:False
booster:True
story_spotlight:False
edhrec_rank:15706
preview.source:Marty Friedman
preview.source_uri:https://twitter.com/marty_friedman/status/1349527247560994816
preview.previewed_at:2021-01-13
prices.usd:0.87
prices.usd_foil:5.68
prices.eur:1.37
prices.eur_foil:2.00
prices.tix:
related_uris.gatherer:https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=503627
related_uris.tcgplayer_decks:https://decks.tcgplayer.com/magic/deck/search?contains=Reidane%2C+God+of+the+Worthy&page=1&utm_campaign=affiliate&utm_medium=api&utm_source=scryfall
related_uris.edhrec:https://edhrec.com/route/?cc=Reidane%2C+God+of+the+Worthy
related_uris.mtgtop8:https://mtgtop8.com/search?MD_check=1&SB_check=1&cards=Reidane%2C+God+of+the+Worthy
purchase_uris.tcgplayer:https://shop.tcgplayer.com/product/productsearch?id=230921&utm_campaign=affiliate&utm_medium=api&utm_source=scryfall
purchase_uris.cardmarket:https://www.cardmarket.com/en/Magic/Products/Singles/Kaldheim/Reidane-God-of-the-Worthy-Valkmira-Protectors-Shield?referrer=scryfall&utm_campaign=card_prices&utm_medium=text&utm_source=scryfall
purchase_uris.cardhoarder:https://www.cardhoarder.com/cards/87365?affiliate_id=scryfall&ref=card-profile&utm_campaign=affiliate&utm_medium=card&utm_source=scryfall

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...