如何解决从JSON解析创建简单结构
这对我来说很混乱,到目前为止我一直找不到答案。
我有我的游戏的派系列表。 此列表存储在.cdb(CastleDB)文件中 .cdb本质上是一种存储.JSON的方式,它具有像.csv
这样的行和列样式编辑器的额外好处。游戏开始时必须加载此JSON(.cdb)文件。
这是我的代码:
#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()
“ Content”变量是已解析的JSON,存储为字典,其结构如下:
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
我不确定如何从此词典中提取所需的信息。 最好每个FactionName和声望Value对都是一个“ Faction”,然后将每个“ Faction”添加到“ Factions”数组中。
我正在尝试构建一个可在编程/运行时使用的临时结构/词典/列表。
我也不确定在程序最终结束时如何重新打包所有这些信息,以便可以保存/覆盖JSON中的信息
这是我尝试简化结构的失败尝试:
#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
解决方法
一方面,GDScript 区分大小写——您需要 for sheet in content["Sheets"]
,而不是 for sheet in content["sheets"]
。
此外,在这段代码中:
var dictionary = {
Name = line,Reputation = line
}
您正在为整个 line
字典分别设置 Name 和 Reputation。您最终会得到 {Name:{FactionName:Gaea,ReputationValue:4000},Reputation:{FactionName:Gaea,ReputationValue:4000}}
。您可以将 line
添加到 FactionArray
。
旁注:您可以使用 content.Sheets
代替 content["Sheets"]
、sheet.name
代替 sheet["name"]
等。它可能更具可读性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。