I know this is not a new topic. I have read the past posts, but still cannot get this to work, even using the examples provided.
I am storing an array (of strings) in a dictionary, then exporting the dictionary to JSON using GenerateJSON. Inspecting the JSON file shows the array of strings propertly formatted.
Then I read file back into a TextInputStream and call ParseJSON (Xojo 2019 r3.1 Mac).
So far, so good, no Exceptions
Here is what happens when I try to access the result of ParseJSON
var x as integer var s as string var d as dictionary var list() as string var restoredData() as variant = ParseJSON( textFromInputStream ) for each dict as Dictionary in restoredData 'OK x = dict.value("keyFor_AnInteger") 'OK s = dict.value("keyFor_AString") 'OK d = dict.value("keyFor_ADictionary") 'OK list = dict.value("keyFor_StringArray") 'TYPE MISMATCH EXCEPTION next
Ok, everything is a variant. I see that in the Docs
But the for…each loop iterator coerces a variant() into a dictionary() behind the scenes.
Not so for the string() stored in the dictionary.
I can view the data on disk, retrieve it, but not reference it. I cannot caste the variant back into a string(), nor loop it.
I have tried:
for each s as string in dict.value("keyFor_StringArray") 'Compiler says: This is not an array but you are using it as one next var retrieved() as string = data.value("keyFor_StringArray") 'Compiles, but for each s as string in retrieved 'TYPE MISMATCH EXCEPTION ... next list = Array(dict.value("keyFor_StringArray")) 'NOPE..compiler says TypeMismatchError var v as variant = dict.value("keyFor_StringArray") if v.isArray then //still cannot refernce the variant as an array end if
There is an obvious trick here, but somehow I can’t see the forest through the trees.
What is the best technique to solve this?
Thanks in advance