JSON text question

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bishop

Hi,…I have for example two objects (planets) ,player ship travels through the universe and
when is close (raycasting from player ship) to the planet I need some info about planet is
shown…so I have Label node for that.
So I wrote json formatted text…something like
name : Planet X25
weight : 250000
info : Planet X25 is an old abandoned planet, there were mining colonies.
name : Planet C678
weight : 120000
info : Planet C678 is mainly covered with lava and stones.
…script for parse json works but I get a whole json text…so how I get for example only info text or weight and info ?
…so far i do not understand the arrays and the dictionary of things,yet.
Thanks for your help.

:bust_in_silhouette: Reply From: SingingApple

By what I understand of your question,

First you need to parse the json file into a dict like this:

var planet_info = {}

func load_json(json):
    var file = File.new()
    file.open(json, File.READ)
    planet_info = parse_json(file.get_as_text())
    file.close()

That should send the json data into the dict.
Then you can access it in the normal way through the dict with the key.

This should work.

Thanks,…but what is json in this script?..variable for a json file?
…I need open “res://planets.json”
so file.open(json, File.READ) do not open anything

Bishop | 2018-06-11 14:58

yep json is the variable for the json file.

SingingApple | 2018-06-11 15:20

Yes…Thanks very much…, could you still show me how to use the dictionary solution?
…I do not get dictionary procedure,yet.

Bishop | 2018-06-11 15:36

Suppose you want to get the weight you can do planet_info[“weight”] . That should work.

SingingApple | 2018-06-11 16:15

Yeah,…now ,it’s works fine…Thanks very much for your help!

Bishop | 2018-06-11 16:55