I'm trying to fetch information about orders from an API for CDON, but I've ran into a problem when I'm trying to iterate through the product rows, and I can't figure out what is causing it.
I'm using json2.asp and here is the code I'm using.
strCallback = getCDONOrder(CDONorderID)
response.write strCallback & "<br><br>" 'This outputs the Json below for reference
set CDONObj = json.parse(strCallback)
for each orderProduct in CDONObj.OrderDetails.OrderRows
response.write orderProduct & "<br>"
next
The strCallback contains correct json-formatted code and the output looks like this (slightly simplified for easier overviewing):
{
"OrderDetails": {
"CustomerInfo": {
"CustomerId": 0,
"ShippingAddress": {
"Name": "Firstname Lastname",
"StreetAddress": "Streetname, 15",
"ZipCode": "12345",
"City": "Cityname",
"Country": "SE"
}
},
"OrderRows": [{
"OrderRowId": 1,
"FulfillmentStatus": "Invoiced",
"PaymentStatus": "AwaitingPayment",
"ProductId": "30761620",
"ProductName": "Test-Product",
"ProductType": "Article",
"Quantity": 1,
"PricePerUnit": 3995.0
}],
"TotalVat": 799.0
}
}
I am able to fetch the quantity with orderProduct.Quantity
on the first iteration, however the For Each
-loop doesn't stop after the first iteration, despite there only being one product. If I try to grab it within the loop it will crash on the second iteration through the loop. If I run the code above it will keep going and create this output:
[object Object]
function(k) { return this[k]; }
function(k,v) { if(typeof(v) === "unknown") { try { v = (new VBArray(v)).toArray(); } catch(e) { return; } } this[k] = v; }
function(k) { delete this[k]; }
function() { var d = new ActiveXObject("Scripting.Dictionary"); for(var key in this) { if(this.hasOwnProperty(key)) { d.add(key, this[key]); } } return d.keys(); }
Now, the first [object Object]
is correct, and contains the product order row we want to look at, but the following rows I don't know where it comes from and why it keeps iterating over them. The code in those 4 rows are from the json2.asp-file.
I have tried putting if isObject(orderProduct) then
within the loop, but it will still output those rows.
What is happening here, and how can I solve it?
EDIT:
Dirty fix to circumvent the problem. I managed to find an if
-statement to put around the inside of the for each
loop. It doesn't stop it from looping, but it does stop it from crashing when trying to fetch the Quantity (and other values) within the loop.
for each orderProduct in CDONObj.OrderDetails.OrderRows
if orderProduct.get("Quantity")<>"" then
...
end if
next
I'm not going to put it down as an answer as it doesn't answer the question about why it's happening, but perhaps the edit can help someone else in a similar situation.
" : next`. The `.keys()` returns indexes of an array object , and names of a name-value collection. – Flakes May 13 '21 at 04:40