Hi there,
Is there any way i can create a dictionary in the Script action?
In Python the syntax would be like this:
dict = {key1: value1, key2: value2}
Thanks
Syntax for dictionary in Script action?
Moderator: Martin
Re: Syntax for dictionary in Script action?
Hi,
are you looking for this?
are you looking for this?
Code: Select all
Map newMapFromValues(String key1, Object value1, ...)
Returns a new map initialized to contain the specified keys and values.
Re: Syntax for dictionary in Script action?
Yes! Thanx
Re: Syntax for dictionary in Script action?
I just learned that Automagic supports JSON.
So we can do the following:
Please note the nesting quotation marks (outer single quotes, double quotes for strings)
This post is just for the record
So we can do the following:
Code: Select all
dict = fromJSON('{"key1": "value1", "key2": "value2", "key3integer": 123, "key4boolean": true}')
This post is just for the record

Re: Syntax for dictionary in Script action?
@Helios : you don't need to create the json, just directly create the map. Map object is equivalent to dictionary in python. I always make it new line for each key, for better indentation.
Each value can be access by querying the key. For example, to access "value2", use dict["key2"]
Code: Select all
dict = newMapFromValues(
"key1", "value1",
"key2", "value2",
"key3integer", 123,
"key4boolean", true);
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Syntax for dictionary in Script action?
@Desmanto You are totally right. In case you need a simple map, the newMapFromValues() function is sufficient.
But in case you want to have a more complex object, e.g.
vs.
I find the first solution better to read.
Of course, by using the second approach with newlines + indention, it will be easier to read - but still - it's more text to type
But in case you want to have a more complex object, e.g.
Code: Select all
obj = fromJSON( '{"key1": [1, 2, 3, 4], "key2": {"sub1": true, "sub2": false} }' )
Code: Select all
obj = newMapFromValues( "key1", newList(1, 2, 3, 4), "key2", newMapFromValues("sub1", true, "sub2", false))
Of course, by using the second approach with newlines + indention, it will be easier to read - but still - it's more text to type
