I want to use a .json file, but I encountered the following issue

extends Node

func _ready():
var file = File.new()
var error = file.open(“res://1_1_2.json”, File.READ)

if error == OK:
    var json_string = file.get_as_text()
    file.close()
    print("File content: ", json_string)
else:
    print("Failed to open file.")

issue: Identifier “File” not declared in the current scope.
Identifier “File” not declared in the current scope.

func read_json():
	var json_as_text = FileAccess.get_file_as_string(leveling_file)
	leveling_data = JSON.parse_string(json_as_text)
1 Like

func load_json(file_path: String):
var error = FileAccess.open(file_path, FileAccess.READ)
if error == null:
print("Failed to open file: ", file_path)
return

var json_string = error.get_as_text()  
error.close()


var json = JSON.new()
var parse_result = json.parse(json_string)

if parse_result.error == OK:
	json_data = json.result
	print("JSON data loaded: ", json_data) 
	total_images = json_data.assets.size()  
	print("Total images set to: ", total_images)
else:
	print("JSON parse error: ", parse_result.error_string)

The following line of code has encountered this issue again:
“if parse_result.error == OK” :Invalid get index ‘error’ (on base: ‘int’)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.