json 如何使用GDscript获取GitHub文件夹中的所有文件?

fivyi3re  于 4个月前  发布在  Git
关注(0)|答案(1)|浏览(80)

我想获取我的GitHub存储库中OnlineLevels/VerifiedLevels/文件夹中每个文件的名称。我使用HTTPRequest节点并从GitHub API获取数据。
下面是我的代码:

func _ready():
    $HTTPRequest.request_completed.connect(_on_request_completed)
    $HTTPRequest.request("https://api.github.com/repos/SuperGames-D/ColorBlocks/git/trees/main?recursive=1")

func _on_request_completed(result, response_code, headers, body):
    var json = JSON.parse_string(body.get_string_from_utf8())
    print(json)

字符串
这就是结果:

{ "sha": "b5d857af5cbe4450855fbf2ef0e96c875f3c08b7", "url": "https://api.github.com/repos/SuperGames-D/ColorBlocks/git/trees/b5d857af5cbe4450855fbf2ef0e96c875f3c08b7", "tree": [{ "path": "OnlineLevels", "mode": "040000", "type": "tree", "sha": "7804f8fafc98a105759074811601e6ebcf84e667", "url": "https://api.github.com/repos/SuperGames-D/ColorBlocks/git/trees/7804f8fafc98a105759074811601e6ebcf84e667" }, { "path": "OnlineLevels/VerifiedLevels", "mode": "040000", "type": "tree", "sha": "73722f67c4161c2100c447008c8d9ef109a82c68", "url": "https://api.github.com/repos/SuperGames-D/ColorBlocks/git/trees/73722f67c4161c2100c447008c8d9ef109a82c68" }, { "path": "OnlineLevels/VerifiedLevels/CBLevel_Ice mode test.json", "mode": "100644", "type": "blob", "sha": "efd553f8f995434029f17ec48e8211c4985cc1df", "size": 2061, "url": "https://api.github.com/repos/SuperGames-D/ColorBlocks/git/blobs/efd553f8f995434029f17ec48e8211c4985cc1df" }], "truncated": false }


我不太擅长JSON,所以这可能是一个简单的问题,但如果有人能帮助我就太好了。
编辑:我发现this page只显示我想要的文件夹的结果。

qmb5sa22

qmb5sa221#

嗯,我想这其实是个简单的问题。
下面是我使用的代码:

func _ready():
    $HTTPRequest.request_completed.connect(_on_request_completed)
    $HTTPRequest.request("https://api.github.com/repos/SuperGames-D/ColorBlocks/git/trees/main?recursive=1")

func _on_request_completed(result, response_code, headers, body):
    $Loading.visible = false
    var json = JSON.parse_string(body.get_string_from_utf8())
    
    var json_tree = json["tree"]
    for i in json_tree:
        var path = i["path"]
        path = path.replace("CBLevel_", "")
        path = path.replace(".json", "")
        if "OnlineLevels/VerifiedLevels/" in path:
            path = path.replace("CBLevel_", "")
            path = path.replace(".json", "")
            path = path.replace("OnlineLevels/VerifiedLevels/", "")
            addLevel(path)

字符串

相关问题