Page 1 of 1

Parse JSON String

PostPosted: Thu 15 Jun 2017, 22:27
by lionelviet
Hello everyone, great open source tool thanks to the contributors!

Something which is blocking me please:

In an action:

1) Parse JSON String "{'text0':'blu','text1':'bla'}" and store it into variable structureDevSaying

2) Do =ToString(Variable(structureDevSaying['text0'])) to the text of DevSayingText

=> My DevSayingText only shows 0. Any idea why? Thanks a lot

Re: Parse JSON String

PostPosted: Thu 15 Jun 2017, 23:18
by ddabrahim
ToString(Variable(structureDevSaying['text0']))=>My DevSayingText only shows 0. Any idea why?

By using the Variable() expression you get the value as number and by using the ToString() expression you only convert the value to string but not getting the value as string. To get the value as string you need to use the VariableString() expression instead of ToString(Variable()) and you also need to make sure the actual value is stored in a string variable.

Also make sure to add the variable to the scene variables using the editor.
Right click on the scene in project manager -> Modify initial variables
Normally, if a variable doesn't exist GDevelop go ahead and create it for you on request and in most cases it helps to avoid errors with "undefined" variables but not always. In some cases even though GDevelop create the variable on request, it won't work and you get a 0 or an empty string returned as the variable is created, but not being used by the action.
By adding a variable to the list of the scene variables, you can avoid this kind of errors.

Finally, to add an empty string variable to the list, just simply leave the initial value field empty. Make sure there is nothing, not even '' or "" signs. If no initial value assigned to a variable, it is going to be created as string variable.
Also, in case you want to add some text as initial value, you can just simply type the text without using '' or "" signs.

Re: Parse JSON String

PostPosted: Sat 17 Jun 2017, 18:50
by lionelviet
Thanks a lot @ddabrahim.

Followed all the steps which make sense, and still not working.

What I don't get is the initial setup for the initial variable structureDevSaying: this is not a string variable, but a structure (i.e. a hash). I tried to leave it totally empty to start with but if this defines the field as a string, it's probably not the right thing.

New tweak is:

Do ="ca marche pas" to the text of variable structureDevSaying.text
Parse JSON string "{'text':'blu'}" and store it into variable structureDevSaying
Do =VariableString(structureDevSaying.text) to the text of DevSyingText

=> shows "ca marche pas" in the scene.

So the second step with the JSON string "{'text':'blu'}" is not working. I tried "{text:'blu'}" or {text:'blu'} or {text:"blu"} without success

Re: Parse JSON String

PostPosted: Sat 17 Jun 2017, 19:16
by lionelviet
I should have checked the C++ code before.. "{\"text\":\"blu\"}" did the trick. Probably worth adding in the doc somewhere, let me know if I can help.

Re: Parse JSON String

PostPosted: Sat 17 Jun 2017, 20:11
by lionelviet
Now trying to find the best way to display a text of variable size, justified inside a Text Object: I tried adding a custom size to the Text Object but the text still displays on one line, overflowing outside of the Object. I see there is a NewLine() method on strings, but given a random string taken from a JSON, I would rather not have to parse every n characters and add a NewLine() operator after each fragment..

Is there any way to add CSS text-align:justify for the TextObject? Or at least to be able to define the new line from the JSON instead?

Thanks

Re: Parse JSON String

PostPosted: Sat 17 Jun 2017, 23:14
by Lizard-13
You can add a "\n" to do a newline just inside the string, check this :) :
(5.97 KiB) Downloaded 25 times