I have a class which has a property which is a string array: as1()
I want to get some Text that has been created and is in the JSON format and use it to fill the as1() array
Writing some code to test the situation, I create a sample Text that is in the JSON format and contains two elements of an array.
It looks like this: [“mouse”, “panda”]. It is assigned to the Text variable sampleJSON for the purposes of testing.
I want to make as1(0) = “mouse” and as1(1) = “panda”
So I try the following code:
Redim Self.as1(-1)
Dim sampleJSON As Text
Const DQ = &u22 // the double quote
sampleJSON = "[" + DQ + "mouse" + DQ + "," + DQ + "panda" + DQ + "]"
Self.as1 = Xojo.Data.ParseJSON(sampleJSON)
I get a type mismatch error. Presumably because Self.as1 is a String array and Xojo.Data.ParseJSON expects an Auto array?
So I experiment with
Dim goat() As Auto
Dim sampleJSON As Text
Const DQ = &u22 // the double quote
sampleJSON = "[" + DQ + "mouse" + DQ + "," + DQ + "panda" + DQ + "]"
goat = Xojo.Data.ParseJSON(sampleJSON)
That works fine. goat(0) = mouse and goat(1) = panda
So I can do the following.
[code]Redim Self.as1(-1)
Dim goat() As Auto
Dim sampleJSON As Text
Const DQ = &u22 // the double quote
sampleJSON = “[” + DQ + “mouse” + DQ + “,” + DQ + “panda” + DQ + “]”
goat = Xojo.Data.ParseJSON(sampleJSON)
For Each item As String In goat()
Self.as1.Append(item)
Next[/code]
This code “works” but seems very inefficient and awkward. I create the array goat and then use it only once to fill as1 and then I can throw it away.
Am I missing something? Is there a more accepted way of filling a String array property with the values contained in some JSON text?