Web Connection
Array from JSON string
Gravatar is a globally recognized avatar based on your email address. Array from JSON string
  Paul Nesci
  All
  Sep 14, 2020 @ 07:20am

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.

Gravatar is a globally recognized avatar based on your email address. re: Array from JSON string
  Rick Strahl
  Paul Nesci
  Sep 14, 2020 @ 12:33pm

.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 ---

Gravatar is a globally recognized avatar based on your email address. re: Array from JSON string
  Paul Nesci
  Rick Strahl
  Sep 14, 2020 @ 05:21pm

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")

Gravatar is a globally recognized avatar based on your email address. re: Array from JSON string
  Paul Nesci
  Paul Nesci
  Sep 14, 2020 @ 05:39pm

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

Gravatar is a globally recognized avatar based on your email address. re: Array from JSON string
  Rick Strahl
  Paul Nesci
  Sep 14, 2020 @ 05:54pm

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 ---

© 1996-2024