Maps and JSON tofile fromfile

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Maps and JSON tofile fromfile

Post by yogi108 » 22 May 2019 07:02

Hi,

I am at a point to store some data and like to use JSON format,
combined with maps into a file.

The flow creates the map:
cMap=newMapFromValues("triggertime",triggertime,"name","Martin","phone","+486145558","info","Info");
js=toJSON(cMap);

The action stores the variable js into the file, all OK.
Next time the flow starts I read the stored map, do a
cMap=from JSON(var);

Now I want to have the next data
cMap1=newMapFromValues("triggertime",triggertime,"name","Desmanto","phone","+626163451","info","Info1");

combined with cMap, stored as JSON again in the file, so now there are 2 records.
How to add cMap and cMap1?

Thanks a lot,
Regards,
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Maps and JSON tofile fromfile

Post by anuraag » 22 May 2019 11:11

There are 2 ways

1st method
Create a list and store cMap in it.

Code: Select all

cList=newList();
cMap=newMapFromValues("triggertime",triggertime,"name","Martin","phone","+486145558","info","Info");
addElement(cList, cMap);
cMap1=newMapFromValues("triggertime",triggertime,"name","Desmanto","phone","+626163451","info","Info1");
addElement(cList, cMap1);
js=toJSON(cList);
2nd method
Create only one cMap and create every key as List.

Code: Select all

cMap=newMapFromValues(
"triggertime", newList(),
"name", newList(),
"phone", newList(),
"info", newList());
Now when you need to add an entry do like this

Code: Select all

addElement(cMap['triggertime'], triggertime);
addElement(cMap['name'], "Martin");
addElement(cMap['phone'], "+486145558");
addElement(cMap['info'], "info");

js=toJSON(cMap)

User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Re: Maps and JSON tofile fromfile

Post by yogi108 » 22 May 2019 11:22

Hi,

It seems to be the 2nd solution I prefer.

Thanks a lot,
Regards

[edit]
after some tests I am at the 1st solution now, got it working :-)
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Maps and JSON tofile fromfile

Post by Desmanto » 25 May 2019 15:47

First solution is better, as you always keep adding the map to the end of the list, no need to care about the key map (cMap1, cMap2, ....)

For 2nd solution, you can create the map as you do (cMap1), and then add the map.
cMap["cMap1"] = cMap1;

This will create the nested map with the key cMap1, cMap2, .... and so on. But might be quite confusing, so better solution 1.
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.

Post Reply