简体   繁体   中英

Nested JSON - Add a Collection

the user https://stackoverflow.com/users/478884/tim-williams already delivered a nice script to make a policy json for the Cardano Blockchain

But to tune the existing script i want to try to group elements of the json to a collection.

Properties should be grouped to a traits:{[]} collection

Sub live2_json()
Dim jsonFileObject As New FileSystemObject
Dim jsonFileExport As TextStream
Dim i, count As Long
Dim root As Dictionary, k As Dictionary, a As Long

Dim tokenname, name, id, description, madeof, rarity, publisher, series, value, ipfshash, bg, plate, cutlery, napkin, temperature, roommates As String

Dim max_count As Double
While ThisWorkbook.Sheets("Tokens").Cells(2 + i, 1).value <> ""
    max_count = 0
    max_count = ThisWorkbook.Sheets("Tokens").Cells(2 + i, 1).value
    
    tokenname = ThisWorkbook.Sheets("Tokens").Cells(2 + i, 2).value

    Set root = New Dictionary
    Set k = New Dictionary
    Set properties = New Dictionary
    set props = New Collection
    root.Add "721", k
    k.Add "policy", New Dictionary
    
    
    Set k = k("policy")
   
    For count = 0 To max_count - 1
        'Tokenname
        tokenname = ThisWorkbook.Sheets(tokenname).Cells(2 + count, 2).value
        k.Add tokenname, New Dictionary

        With k(tokenname)
            id = ThisWorkbook.Sheets(tokenname).Cells(2 + count, 4).value
            'Name
            name = ThisWorkbook.Sheets(tokenname).Cells(2 + count, 3).value

            .Add "Name", name
            .Add "Id", id

            
        End With

        k.Add tokenname, properties
        properties.Add props
        props.Add "prop1", "blue"
        props.Add "prop2", "red"
    Next
    
    
    i = i + 1000
Wend

    Set jsonFileExport = jsonFileObject.CreateTextFile("C:\1_DEV\BIGjsonExample.json", True)
    jsonFileExport.WriteLine (JsonConverter.ConvertToJson(root, Whitespace:=2))

unfortunatly this collection is added at the end of the json instead in the tokenname section. If i put it into the for-next it crushes:/

this is the structure i want to build and used as a reference

{
  "721": {
    "1131301ad4b3cb7deaddbc8f03f77189082a5738c0167e1772233097": {
      "CardanoBits4792": {
        "image": "ipfs://ipfs/QmRXMuJQnUfrkYASW4Ur2dinTAKQ5mJ41ZspnQjxt3MRw7",
        "name": "CardanoBits #4792",
        "properties": [
          {
            "key": "type",
            "value": "human"
          }
        ],
        "tags": [
          "male",
          "brown-skin",
          "red-shirt",
          "ugly-blonde",
          "bloody"
        ]
      }
    }
  }
}

ok i made it missing part was:

            .Add "Properties", New Collection  
        End With

            
            k(tokenname)("Properties").Add "prop1: " & "test"

        

thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM