JSON array into auto - can't access the resulting array

  1. 3 months ago

    Neil M

    Mar 13 Pre-Release Testers, Xojo Pro
    Edited 3 months ago

    Hi, I'm having an issue parsing some JSON which produces what looks like an array of arrays.

    I could manually process this using string manipulation quickly and easily but I would like to do it the 'proper' native Xojo way. Also I will be using JSON data a lot more in future so I would like to know how to access this structure properly.

    This is some of my test code that demonstrates the issue I'm having. The JSON array format is not really user friendly but it's what I'm stuck with.

    The data appears to be processed correctly and shows up as being an array in the debugger when I look at the current variables at a breakpoint.

    Dim jsonData As text
    jsonData = "[[3,303,3.003,333333333333,30003.30000003],[7,707,7.007,777777777777,70007.70000007],[5,505,5.005,555555555555,50005.50000005],[9,909,9.009,999999999999,90009.90000009]]"
    dim parsedarray() as auto
    parsedarray = Xojo.Data.ParseJSON(jsonData)
    
    // Iterate through each 'JSON' array entry in 'parsedarray'
    for each record as auto in parsedarray
      
      // DEBUG: What type do we have in record
      Dim info As Xojo.Introspection.TypeInfo
      info = Xojo.Introspection.GetType(record)
      // info shows that 'record'  is an array at this stage
      // BaseType Nil, FullName Auto(), HasElementType True, IsArray True
      // All other variables in the info variable are False apart from the last one Name which is Auto()
    
    	// When I attempt to access 'record' as an array it won't run. It shows up as an array but I don't know how to access it
      
      dim a as integer
      // The following line is where it fails with the error shown underneath the code snippet
      a = record(1)
      
      break
    next

    The error when I try to run it is :

    "This is not an array but you are using it as one"
    a = record(1)

    I've looked around on the forum and couldn't find any solution to this. This JSON format is based on the format that comes out of the Binance API although I have changed the values to numbers to make them easy to replicate, it's valid JSON.

    I'm sure it's something to do with the 'auto' type, what exactly I'm not sure of which is my issue here.

    Does anyone have any ideas how I can get the data out of the record array?

    The problem is that record is dim'd as Auto, which is not an array. Before attempting to access it, assign it to an array, something like this:

    for each record...
      dim arr() as auto = record
      dim a as integer = arr( 0 )
  2. Kem T

    Mar 13 Pre-Release Testers, Xojo Pro, XDC Speakers Answer New York

    The problem is that record is dim'd as Auto, which is not an array. Before attempting to access it, assign it to an array, something like this:

    for each record...
      dim arr() as auto = record
      dim a as integer = arr( 0 )
  3. Paul L

    Mar 13 Xojo Inc http://docs.xojo.com
    Edited 3 months ago

    You need to assign record to an Auto array:

    Dim a() As Auto
    a = record
    For Each i As Auto In a
      Dim myValue As Double = i
    Next

    Edit: Kem beat me to it!

  4. Neil M

    Mar 13 Pre-Release Testers, Xojo Pro

    Thanks guys, that worked perfectly!

or Sign Up to reply!