rcdmk / aspJSON

A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
MIT License
203 stars 89 forks source link

Loop through objects to get the property name #95

Closed bcmartinez closed 4 years ago

bcmartinez commented 4 years ago

Hello! Is it possible to loop through properties and get property names? For example, here is the format of the json I'm dealing with (which comes from an API that I do not control):

{
  "projectTypes": {
    "Project Template": {
        "name": "My Template Name",
        "ID": "123"
    }
  }
}

I don't know any of the property names in advance, so I'd like to loop through them to display them on my page. Is it possible to do something like objJson(0) or objJson(0).name instead of objJson("projectTypes")?

rcdmk commented 4 years ago

Hello! The JSONobject class has a property that gives you access to the collection of JSON properties:

<%
Dim json, projects, project, template

Set json = New JSONobject
json.Parse(jsonString)

projects = json("projectTypes").pairs ' this is the list of name-value pairs that store the parsed JSON data properties

' you can iterate the pairs with regular for or for each loops
For Each project In projects
    Response.Write project.name & ": " ' this is the key for the each child and will write "Project Template: " in that example

    Set template = project.value ' this is the child JSONobject
    Response.Write template("name") & " - " & template("ID") & "<br>" ' this will write "My Template Name - 123<br>"
Next
%>
bcmartinez commented 4 years ago

@rcdmk - Thank you!