Getting JSON array data into a string array property

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?

Nope, that’s it.

But note: according to the documentation, For Each does not guarantee order. If that’s important, cycle through the array by index instead.

Otherwise, if it’s a big array, this might be more efficient, but I doubt by much:

// your code up to getting goat

redim self.as1( goat.Ubound )
for index as integer = 0 to goat.Ubound
  self.as1( index ) = goat( index )
next

Kem, I appreciate the warning about order which is in fact important to me. I would have spun around that issue for a while…

I doubt that. As I mentioned, the documentation says that, but in practice, it always seems to work in order anyway. I always bring it up just in case Xojo changes the underlying code sometime in the future that leads to out-of-order processing, but until then…

Personally, I hope they just decide to change the documentation. :slight_smile: