OutOfBoundsException on JSONItem

Why do I get an OutOfBoundsException when the JSONItem contains 2 objects?

[code] Dim data As String = DefineEncoding(content, Encodings.UTF8)
Dim jChild As new JSONItem(data)
Dim jNode As JSONItem
Dim msg As String

for i As Integer = 0 to jChild.Count-1
jNode = jChild.Child(i) <<---- OutOfBoundsException
msg = msg + “ID” + jNode.Value(“id”) + ", Email: " + jNode.Value(“email”) + Chr(13)
next

MsgBox msg[/code]

Response received from API (data String variable)
{
“object”: “list”,
“count”: 2,
“url”: “/v1/customers”,
“data”: [
{
“object”: “customer”,
“created”: 1389910508,
“id”: “cus_3K2UBxudhu8Hrt”,
“livemode”: false,
“description”: “Customer 1”,
“email”: “cust1@mydomain.com”,
“delinquent”: false,
“metadata”: {},
“subscription”: null,
“discount”: null,
“account_balance”: 0,
“currency”: null,
“cards”: {
“object”: “list”,
“count”: 0,
“url”: “/v1/customers/cus_3K2UBxudhu8Hrt/cards”,
“data”: []
},
“default_card”: null
},
{
“object”: “customer”,
“created”: 1389910386,
“id”: “cus_3K2S9rTs5KTs62”,
“livemode”: false,
“description”: “Customer 2”,
“email”: “cust2@mydomain.com”,
“delinquent”: false,
“metadata”: {},
“subscription”: null,
“discount”: null,
“account_balance”: 0,
“currency”: null,
“cards”: {
“object”: “list”,
“count”: 0,
“url”: “/v1/customers/cus_3K2S9rTs5KTs62/cards”,
“data”: []
},
“default_card”: null
}
]
}

With your for loop you are testing the wrong count. Since you are using jChild.Child(index) you need to get the count of the jChild.Child array. You need to change your for loop to:

for i As Integer = 0 to jChild.Child.Count-1 jNode = jChild.Child(i) msg = msg + "ID" + jNode.Value("id") + ", Email: " + jNode.Value("email") + Chr(13) next

I now get an error on compile: “This method requires more parameters than were passed”

jChild.Child(0).Count-1 ?

Still receiving OutOfBoundsException

Interesting thing. jChild.IsArray is returning False even though an array of customer records are returned by the API.

jChild contains 4 Values, one of which is an array.

dim jCustomers as JSONItem = jChild.Value("data")
for i = 0 to jCustomers.Count-1
  ...

As Tim said, something like this should work:

[code]
Dim data As String = DefineEncoding(kJSON, Encodings.UTF8)
Dim jChild As new JSONItem(data)
Dim jNode As JSONItem
Dim msg As String

jNode = jChild.Value(“data”)

Dim jc As JSONItem
For n As Integer = 0 To jNode.Count-1
jc = jNode.Value(n)

msg = "ID = " + jc.Value("id") + ", Email: " + jc.Value("email")
MsgBox(msg)

Next[/code]

Perfect. Thank you.