Hi Rick (and all)
Having a bit of trouble working out how to extract the "Saleitems" array from this incoming JSON packet:
{"CreateSale": {
"AuthToken": "Some token here",
"MemberNo": "05301",
"Account": 2,
"Reference": 123444,
"Terminal": 17,
"PostDate": "2020-08-13T12:34:56",
"Description": "POS Sale",
"SaleItems": [
{
"Description": "FOOD",
"Amount": 4.00,
"TaxAmount": 0.36,
"ItemCode": 234
},
{
"Description": "BEVERAGE",
"Amount": 4.40,
"TaxAmount": 0.40,
"ItemCode": 456
}
]
}}
No issues deserialization this far:
lcJRaw = SUBSTR(Request.cformvars,2)
loJData = loSer.DeserializeJson(lcJRaw)
lcMemberNo = PADL(ALLTRIM(loJData.CreateSale.MemberNo),5,"0")
But now how do I access "SaleItems[]"? I am just not making any sense of UTILS section of DOCS in WC...
Thanks in Advance Paul PS: Look forward to hitting up the new Logs you built, nice.
.SaleItems
is a collection, so you can reference as such:
loSaleItems = loRoot.CreateSale.SaleItems
loSaleItem = loSaleItems[1]
? loSaleItem.Description
Note that you have to de-reference the Collection (ie. assign loSaleItems
) as FoxPro doesn't allow the dot syntax through arrays and collections so they have to be stuck into a variable.
+++ Rick ---
Hey Rick I am still getting a "Unknown LOSALEITEMS - Undefined" caused by this statement -
loSaleItem = loSaleItems[1]
Had tried creating it first (as below) but no joy?
loSaleItems = CREATEOBJECT("EMPTY")
LOL- majestic fluke
loSaleItems = loJData.CreateSale
loSaleItem = loSaleItems.SaleItems(1)
WAIT WINDOW "Description:" + loSaleItem.Description
That works! Thanks for helping, My problem was probably the fact I was trying .SaleItems(0) thinking it a .NET thing but as it a FP Var (0) does not exist... Regards Paul
The collection is a plain FoxPro collection, so not .NET and thus 1 based.
Most likely you won't reference by index but FOREACH FOXOBJECT
over the list anyway.
I would make sure to de-reference the collection into it's own variable. FoxPro's inconsistent support for indexers down the object hierarchy can cause confusing errors otherwise. I try to always explicitly assign the collection and each item retrieved rather than referencing the collection items by index (other than the one required call). Mainly because I can never remember exactly what it is that doesn't work (is it the collection or the indexer that breaks the .
chain) - I don't want have to parse that out each time I look at the code 😃
+++ Rick ---