简体   繁体   中英

Create Simple Struct from JSON parse

This is confusing for me and I have been unable to find an answer so far.

I have a list of Factions for my game. This list is stored in a .cdb (CastleDB) file .cdb is essentially a way of storing .JSON, with the added benefit of having a rows and columns style editor like .csv

This JSON (.cdb) file must be loaded when the game starts.

Here is my code for that:

#Attempt to load from cdb Database.
    var data = File.new()
    data.open(FactionDB, File.READ)
    var content = parse_json(data.get_as_text())
    data.close()

The "Content" variable is the parsed JSON, stored as a dictionary, which has the following structure:

Sheets
    0
        name : Factions
        columns
            0
                typeStr : 0
                name: FactionName
            1
                typeStr : 3
                name: ReputationValue
        lines
            0
                "FactionName" : Gaea
                "ReputationValue" : 4000
            1
                "FactionName" : Wretched
                "ReputationValue" : 0
            2
                "FactionName" : Naya
                "ReputationValue" : 0
            3
                "FactionName" : Amari
                "ReputationValue" : 12000
            4
                "FactionName" : Proa
                "ReputationValue" : 12000
            5
                "FactionName" : Player
                "ReputationValue" : 12000
            6

        separators
        props
customTypes
compress : false

I am unsure how to extract the required information from this dictionary. Preferably, every FactionName and ReputationValue pair would be a "Faction" and then, each "Faction" would be added to a "Factions" array.

I'm attempting to construct a temporary struct/dictionary/list that can be used during programming/runtime.

and I am also unsure how to repack all of this information when my program eventually ends, so that the information in the JSON can be saved/overwritten

Here is my failed attempt at attempting to make a simpler structure:

#for every sheet in the database
    #if the sheet name is "Factions"
    #then for every column in the Factions sheet
    #if the column name is FactionName 
    #NewEntry is duplicate the Faction
    #erase the Factions Name from the faction. 
    #The Factions Name is the NewEntry
    
    for sheet in content["sheets"]:
        if sheet["name"] == "Factions":
            for line in sheet["lines"]:
                if 'FactionName' in line:
                    var dictionary = {
                        Name = line,
                        Reputation = line
                        }
                    FactionArray.push_back(dictionary)
                #var new_entry = entry.duplicate()
                #new_entry.erase("FactionName")
                #FactionData[entry["FactionName"]] = new_entry

For one thing, GDScript is case sensitive--you need for sheet in content["Sheets"] , not for sheet in content["sheets"] .

Also, in this code:

var dictionary = {
    Name = line,
    Reputation = line
}

You're setting Name and Reputation each to the entire line dictionary. You'll end up with {Name:{FactionName:Gaea, ReputationValue:4000}, Reputation:{FactionName:Gaea, ReputationValue:4000}} . You could just add line to FactionArray .


Side note: You can use content.Sheets instead of content["Sheets"] , sheet.name instead of sheet["name"] , etc. It might be more readable.

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