简体   繁体   中英

Looping though JSON classic ASP

I am trying to use aspJSON1.17.asp to get data from an JSON file that I am pulling back. ASPJson Site is located here now. http://web.archive.org/web/20160109181310/http://aspjson.com/

The structure of the file is as follows. NOTE: I have truncated here for readability

{
          "@odata.context": "http://trestle.corelogic.com/odata/$metadata#Property/CoreLogic.DataStandard.RESO.DD_1_5.Property",
          "value": [
            {
              "@odata.type": "#CoreLogic.DataStandard.RESO.DD_1_5.Property",
              "AboveGradeFinishedArea": null,
              "AboveGradeFinishedAreaSource": null,
              "AboveGradeFinishedAreaUnits": null,
              "AccessCode": null,
              "AccessibilityFeatures": null,
              "AdditionalParcelsDescription": null,
              "AdditionalParcelsYN": null,
              "AnchorsCoTenants": null,
              "Appliances": null,
              "ApprovalStatus": null,
              "ArchitecturalStyle": null,
              "AssociationAmenities": null,
              "AssociationFee": null,
              "AssociationFee2": null,
              "AssociationFee2Frequency": null,
              "AssociationFeeFrequency": null,
              "AssociationFeeIncludes": null,
              "AssociationName": null,
              "AssociationName2": null,
              "AssociationPhone": null,
              "AssociationPhone2": null,
              "AssociationYN": null,
              "AttachedGarageYN": null,
              "AvailabilityDate": null,
              "Basement": null,
              "BathroomsFull": 10
    }
  ]
}

I have tried the following where pagereturn is the JSON formatted string I am getting back from the API. I am trying to loop though this collection but I cannot get it to work. I keep getting an Object not a collection error.

Here is what I have tried

Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON(pageReturn)

'Loop through collection
For Each record In oJSON.data("Property")
    Set this = oJSON.data("Property").item(record)
    Response.Write _
    this.item("bathroomsFull") & "<br> "
Next

AND

'Loop through collection
For Each record In oJSON.data("bathroomsFull")
    Set this = oJSON.data("bathroomsFull").item(record)
    Response.Write _
    this.item("bathroomsFull") & "<br> "
Next

This did not give me an error but I got an empty sting

Response.Write "BathRooms"& oJSON.data("bathroomsFull") & "<br>"

Based on the above I would have thought that I would get "10"

Any help would be appreciated.

It's just a case of understanding the structure of the json, then you can recreate the steps needed to pull the value in code.

The structure appears to be;

object
  property
    collection
      object
        property

So the code should be something like this;

Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON(pageReturn)

'Loop through collection
For Each record In oJSON.data("value")
  'Object Reference within the Array.
  Set this = oJSON.data("value").item(record)
  'Use the Object Reference to access it's properties.
  Response.Write this.item("BathroomsFull") & "<br> "
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM