1

How should I retrieve PitcherID from this JSON? I am using the class from http://aspjson.com.

JSON

[
 {
  "PitcherID": "456068"
 },
 {
  "PitcherID": "431148"
 }
]

Code

oJSON.loadJSON("...")

For Each thing In oJSON.data("PitcherID")
    Set this = oJSON.data("PitcherID").item(thing)
    response.write this.item("PitcherID")
Next

Error

Microsoft VBScript runtime error '800a01c3'

Object not a collection
localhost
  • 1,062
  • 3
  • 15
  • 35
  • The error is is telling you that the object you are trying to read as a collection my guess would be `oJSON.data("PitcherID")` in `For Each thing In oJSON.data("PitcherID")` is not a collection object which in this case `PitcherID` isn't if you look at the JSON structure. – user692942 Jun 01 '15 at 12:28

2 Answers2

2

In my experience it's far easier to just use JScript as your server side scripting language than to use the aspjson class. You could render your JSON object as follows

<%@language="javascript"%>
<!DOCTYPE html>
<html>
<body>


    <%
    var oJSON =[
     {
      "PitcherID": "456068"
     },
     {
      "PitcherID": "431148"
     }
    ]
    for (i in oJSON)
    { 
     Response.write((oJSON[i].PitcherID) + "<br />");
    } 
    %>

</body>
</html>

I realise that this may cause problems if the json processing is only part of a page and the rest of it uses VBScript, however you can execute server side JS in what is otherwise a VBS page by using <script runat="server"> eg

<%@language="VBScript"%>
<!DOCTYPE html>
<html>
<head>

<script language="javascript" runat="server">
var oJSON =[
 {
  "PitcherID": "456068"
 },
 {
  "PitcherID": "431148"
 }
]
var strout = ""
for (i in oJSON)
{ 
 strout = strout + ((oJSON[i].PitcherID) + "<br />");
} 
</script>


</head>
<body>

<% Response.write strout %>

</body>
</html>
John
  • 4,658
  • 2
  • 14
  • 23
1

The problem is the class from http://aspjson.com is limited and personally I've always found it hard to find decent examples of how to use it.

Why do you get the Object not a collection error?

It's quite simple really the object you are trying to iterate through like an array / collection is not one.

This line

For Each thing In oJSON.data("PitcherID")

will fail because oJSON.data("PitcherID") is not a collection object which means you cannot iterate through it. For PitcherID to be enumerable the source JSON structure would look more like this

{
  "PitcherID": [
    "456068",
    "431148"
  ]
}

for example.


Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • How do I write the Pitcher ID to my page then? – localhost Jun 01 '15 at 14:24
  • @localhost Your top level is an array *(unnamed)*. Do you have control over the JSON structure? If so modify the JSON so it contains a top level object with a named collection, something like `data: [ { "PitcherID": "456068" }, { "PitcherID": "431148" } ];`. Then you should be able to call it in a `For` loop like `For Each thing In oJSON.data("data")` and use your existing code. – user692942 Jun 01 '15 at 14:31