简体   繁体   English

在 Godot (gdscript) 中,它在解析 JSON 文件时出错,并且在某处需要一个逗号,“调用:在第 2 行解析 JSON 时出错:预期为‘,’

[英]In Godot (gdscript) It has an error parsing a JSON file and it expects a comma somewhere, "call: Error parsing JSON at line 2: Expected ','

heres my code: extends CanvasLayer这是我的代码:扩展 CanvasLayer

export(String, FILE, "*.json") var d_file

var dialogue = []

func _ready():
start()

func start():
dialogue = load_dialogue()

$NinePatchRect/Name.text = dialogue[0]['name']
$NinePatchRect/Chat.text = dialogue[0]['text']

func load_dialogue():
    var file = File.new()
    if file.file_exists(d_file):
        file.open(d_file, file.READ)
                return parse_json(file.get_as_text())

and the video I'm following is this one: https://youtu.be/7CCofjq_dHM我正在关注的视频是这个: https://youtu.be/7CCofjq_dHM

I tried many thing explained in the video but nothing works.我尝试了视频中解释的许多事情,但没有任何效果。 I'm expecting it to parse a JSON file with text inside and make dialogue for a NPC.我期待它解析一个 JSON 文件,里面有文本并为 NPC 进行对话。

So, on this Json data:因此,在这个 Json 数据上:

[ {"name": "Blacksmith", "text": "OH! A survivor! I'm not the only one here!"} {"name": "you", "text": "uh, yeah! I am."} {"name": "Blacksmith", "text": "ah yes, I forgot. I'm a blacksmith. Not a NPC or anything..."} {"name": "you", "text": "..."} {"name": "Blacksmith", "text": "Well, anyway, I'm here so you can get better guns, kill zombos to get gun parts and come back here!"} {"name": "you", "text": "Ok."} {"name": "Blacksmith", "text": "anyway, off you go, bye!"} {"name": "GAME", "text": "(you were kicked out.)"} ]

Let me fix spacing a bit…让我稍微修正一下间距……

[
 {"name": "Blacksmith", "text": "OH! A survivor! I'm not the only one here!"}
 {"name": "you", "text": "uh, yeah! I am."}
 {"name": "Blacksmith", "text": "ah yes, I forgot. I'm a blacksmith. Not a NPC or anything..."}
 {"name": "you", "text": "..."}
 {"name": "Blacksmith", "text": "Well, anyway, I'm here so you can get better guns, kill zombos to get gun parts and come back here!"}
 {"name": "you", "text": "Ok."}
 {"name": "Blacksmith", "text": "anyway, off you go, bye!"}
 {"name": "GAME", "text": "(you were kicked out.)"}
]

You can an array, which is delimited by [ and ] .您可以是一个数组,由[]分隔。 And it has objects, which are delimited by { and } ... But arrays should be comma separated.它有对象,由{}分隔......但是 arrays 应该用逗号分隔。 So it should look like this:所以它应该是这样的:

[
 {"name": "Blacksmith", "text": "OH! A survivor! I'm not the only one here!"},
 {"name": "you", "text": "uh, yeah! I am."},
 {"name": "Blacksmith", "text": "ah yes, I forgot. I'm a blacksmith. Not a NPC or anything..."},
 {"name": "you", "text": "..."},
 {"name": "Blacksmith", "text": "Well, anyway, I'm here so you can get better guns, kill zombos to get gun parts and come back here!"},
 {"name": "you", "text": "Ok."},
 {"name": "Blacksmith", "text": "anyway, off you go, bye!"},
 {"name": "GAME", "text": "(you were kicked out.)"}
]

There the elements of the array (ie the objects) have commas (",") between them.数组的元素(即对象)之间有逗号 (",")。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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