Archived Forum Post

Index of archived forum posts

Question:

Error creating JSON with JsonArray

Apr 27 '16 at 18:51

I am trying to create a json array like the one from DataTables.net.

Set oJ = Server.CreateObject("Chilkat_9_5_0.JsonArray")
Response.Write oJ.version       'returns 9.5.0.58
s = oJ.AddArrayAt(-1)       'returns 0

'   First Child
Set oChild = oJ.ArrayAt(0)
Response.Write TypeName(oChild)       'returns 'Nothing'

Answer

Thanks, I'll give it a try. (I thought I had fixed this issue, but maybe not. I'll double-check..)


Answer

The issue I was thinking about was not the same.

This was not properly documented, and I'll try to make this clear in the documentation: The JsonArray object is not a standalone object. Semantically, it should never be created via CreateObject. It must always be part of a JsonObject For example, examine the JSON spec here: http://www.json.org/

The JSON object is the top-level construct of any JSON document. An array is one particular type of value that can be held within a JSON object.

Therefore, you would do this:

Set oJ = Server.CreateObject("Chilkat_9_5_0.JsonObject")
s = oJ.AddArrayAt(-1)
Set oChild = oJ.ArrayAt(0)
Response.Write TypeName(oChild)

' To add an array inside the array: s = oChild.AddArrayAt(-1) Set oChild2 = oChild.ArrayAt(0) Response.Write TypeName(oChild2)


Answer

chilkat, I this there is an issue with JsonObject.ArrayAt. First is requires a second argument where JsonArray.ArrayAt only takes the index.

Set oJ = Server.CreateObject("Chilkat_9_5_0.JsonObject")
oJ.AddArrayAt -1, ""  <- Extra Argument
Set oChild = oJ.ArrayAt(0)

' To add an array inside the array:
oChild.AddArrayAt -1
Set oChild2 = oChild.ArrayAt(0)
oChild2.AddStringAt -1, "One"
oChild2.AddStringAt -1, "Two"
oChild2.AddStringAt -1, "Three"

The output is this.

{                <- I think the key/value on the line below is changing this it a object
  "": [          <- the extra argument shows here
    [
      "One",
      "Two",
      "Three"
    ]
  ]
}

Answer

Yes, I made a mistake in my first response. JsonObject.AddArrayAt takes two arguments: the index and name. JsonArray,.AddArray At only has 1 argument: the index.

    Set oJ = Server.CreateObject("Chilkat_9_5_0.JsonObject")
    oJ.AddArrayAt -1, "abc" 
    Set oChild = oJ.ArrayAt(0)

' To add an array inside the array:
    oChild.AddArrayAt -1
    Set oChild2 = oChild.ArrayAt(0)
    oChild2.AddStringAt -1, "One"
    oChild2.AddStringAt -1, "Two"
    oChild2.AddStringAt -1, "Three"
The output is this.
    {               
      "abc": [         
        [
          "One",
          "Two",
          "Three"
        ]
      ]
    }
  

Answer

Is there a way to get the first array unnamed? I am looking to create an array or arrays

Something like this?

{               
  [         
    [
      "One",
      "Two",
      "Three"
    ],
    [
      "apple",
      "banana",
      "orange"
    ]
  ]
}

Answer

Okay, I think I understand. I looked at JSON Validator and changed my string until it was happy. This is what I came up with and it happens to match the example data at Data Tables. Can this be done with JSONObject?

[
    [
        "One",
        "Two",
        "Three"
    ]
    ,
    [
        "apple",
        "banana",
        "orange"
    ]

]

Answer

Here's a new build (pre-release) where I've added the Emit method to JsonArray:

32-bit Download: http://www.chilkatsoft.com/download/preRelease/ChilkatAx-9.5.0-win32.zip
64-bit Download: http://www.chilkatsoft.com/download/preRelease/ChilkatAx-9.5.0-x64.zip

(The JsonArray object is obtained from the JsonObject that contains it. If you wish to emit non-compact JSON from JsonArray, set the JsonOjbect.EmitCompact = 0 prior to getting the JsonArray object.)