Can JSONitem store a Xojo array?

Can a JSONitem really store a Xojo array?

The Xojo doc for JSONitem says:

[quote]JSON Objects can contain named data a collection of name-value pairs (like a Dictionary)…
Values can be any of the following types: Strings, numbers, JSONItems, arrays, Booleans, or Nil[/quote]

But whenever I put an array into a JSONitem the subsequent JSONitem.toString gives me a Type Mismatch Exception

Is this a bug in Xojo, the docs or my head?

(I understand the workaround is to convert my array into an array style JSONitem, but the docs say arrays are supported directly… are they really?)

Thanks, Joe

This slightly modified example from the Xojo docs shows the issue:

[code]dim Person as new JSONItem
//This object is manipulated like a dictionary
Person.Value(“Name”) = “John Doe”
Person.Value(“Age”) = 32
Person.Value(“Married”) = True

//Trying to store an actual Xojo Array causes ToString to throw a Type Mismatch Exception below.
Person.Value(“KidsArray”) = Array(“John Jr”, “Jamie”, “Jonah”)

//Convert to a JSON String
dim s as String = Person.toString() //Type Mismatch Exception occurs here if the JSONitem contains a string array[/code]

When you use YourJSONItem.Value(x) = y you are creating a single value. When you use YouJSONItem.Append y you are creating an array.

Create a JSONItem and then loop through your array and append each array member to it.

I think the docs are misleading and what it means is that you can spend another JSONItem that is acting as an array.

If to use the new framework, you can append an array directly.

I think Joe meant this:

  Dim jsString As String
  Dim myArray() As String
  Dim myJSONItem As New JSONItem
  myArray = Array("james", "jack", "john")
  myJSONItem.Value("somestring") = myArray
  // Then
  jsString = myJSONItem.ToString
  // jsString should be: {"somestring":["james", "jack", "john"]}, but it's not, type mismatch error.
  // Instead, you can use JSONItems as arrays:
  Dim myJSONItemArray As New JSONItem
  myJSONItemArray.Append("Can't be bothered")
  // Then it will work:
  myJSONItem.Value("somestring") = myJSONItemArray
  jsString = myJSONItem.ToString 
  // jsString IS: {"some string":["Jenny", "Beth", "Can't be bothered"]}

So, yes, JSONItem can’t be assigned a XOJO array. You can extend it though and handle the array etc.