classes/class_json #248
Replies: 7 comments 5 replies
-
Will it ever have built-in support for YAML or JSON5? |
Beta Was this translation helpful? Give feedback.
-
I am sorry, but this help is such a complete mess. var data_to_send = ["a", "b", "c"]
var json_string = JSON.stringify(data_to_send)
# Save data
# ...
# Retrieve data
var json = JSON.new()
var error = json.parse(json_string)
if error == OK:
var data_received = json.data
if typeof(data_received) == TYPE_ARRAY:
print(data_received) # Prints array
else:
print("Unexpected data")
else:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) Why would you first save data and then load it again? This doesnt make sense. Examples should state both loading and saving. |
Beta Was this translation helpful? Give feedback.
-
Also this line doesnt make any sense: if error == OK:
var data_received = json.data
if typeof(data_received) == TYPE_ARRAY:
print(data_received) # Prints array
else:
print("Unexpected data") Why would you ask if the data is an array, if you want a json-file? |
Beta Was this translation helpful? Give feedback.
-
A common use case here is to read JSON from a file, which the documentation here does not address. Suppose you have a file containing a list of names, split into first and last names at res://Data/names.json: {
"file_name": "names.json"
"first_names" : ["Arthur", "James", "Annie", "Jane"]
"last_names" : ["Smith", "Edwards", "Thomas"]
} # get_names.gd
# Get name data in the form of a dictionary
var json = JSON.new()
var names_string= FileAccess.get_file_as_string("res://Data/names.json")
var names_as_dict= JSON.parse_string(names_string)
var example_name = names_as_dict["first_names"].pick_random() + " " + names_as_dict["last_names"].pick_random() |
Beta Was this translation helpful? Give feedback.
-
How can I do this from Godot/CSharp? |
Beta Was this translation helpful? Give feedback.
-
var data_to_send = ["a", "b", "c"]
var json_string = JSON.stringify(data_to_send)
# Save data
# ...
# Retrieve data
var json = JSON.new()
var error = json.parse(json_string)
if error == OK:
var data_received = json.data
if typeof(data_received) == TYPE_ARRAY:
print(data_received) # Prints array
else:
print("Unexpected data")
else:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) Now, shouldn't we be doing JSON.new() here? var data_to_send = ["a", "b", "c"]
var json_string = JSON.stringify(data_to_send)
# Save data
# ...
# Retrieve data
var parse_result = JSON.parse_string(json_string)
if parse_result.error == OK:
var data_received = parse_result.result
if typeof(data_received) == TYPE_ARRAY:
print(data_received) # Prints array
else:
print("Unexpected data")
else:
print("JSON Parse Error: ", parse_result.error_string) |
Beta Was this translation helpful? Give feedback.
-
So I don't see it anywhere in the docs but it seems |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
classes/class_json
Inherits: Resource< RefCounted< Object Helper class for creating and parsing JSON data. Description: The JSON class enables all data types to be converted to and from a JSON string. This is useful ...
https://docs.godotengine.org/en/stable/classes/class_json.html
Beta Was this translation helpful? Give feedback.
All reactions