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

Hoy to read array and nested objects #97

Closed garolgarel closed 3 years ago

garolgarel commented 3 years ago

Hi!! I'm using your class and is in perfect working, but i can't access data that receive in a Json with an array with nested objects. I was tried to use varius examples of you and other developers here but with bad luck. I need to read the array "method" and his simple items and nested object in a bucle. The "method" is present in two parts of de Json (the Json is provided externally, I can't modify the names), one is above "payments" and the other above "discounts" I need to access and read values from "methods"-->"type" and "amount" and of course "additionalInfo"-->"millas", "voucherCode"...etc I will appreciate your response. Thank you very much!

Paste the Json sample here:

{ "id": 27291931, "createdDate": "2020-06-25T14:42:03.000-04:00", "payer": { "email": "pepe@pepe.com", "id": "12345as" }, "payment":{ "total": 450.01, "methods":[ { "type": 1, "amount": 30.01, "aditionalInfo": { "millas": 20, "voucherCode": "ABC1234" } }, { "type": 2, "amount": 220.01, "aditionalInfo": { "firstSixDigits": "417006", "lastFourDigits": "8020", "expirationMonth": 11, "expirationYear": 2025, "cardholderName": "Alan Mateus", "cardholderNumber": "49684611"
} }, { "type": 3, "amount": 200.01, "aditionalInfo": { "firstSixDigits": "417006", "lastFourDigits": "8020", "expirationMonth": 11, "expirationYear": 2025, "cardholderName": "Alan mateus", "cardholderNumber": "49684611"
} } ]}, "discounts": { "total": 50.01, "methods":[ { "type": 1, "amount": 50.01, "aditionalInfo": { "voucherCode": "123ABC", "voucherType": "money" } } ]} }

rcdmk commented 3 years ago

Hi @garolgarel, thank you for reaching out. I hope you already found your answer, but if not, then I can help you.

Nested JSON arrays and objects are represented as they object counterparts, JSONObject and JSONArray classes.

<%
jsonString = "{"id": 27291931,...}" ' your JSON input here
Set transactions = new JSONObject
transactions.Parse jsonString

Response.Write transactions("id") ' writes the ID property on the root object

' ...

Set methods = transactions("methods") ' this is a JSONArray object

ForEach method In methods.Items
    Response.Write method("type")
    ' ...
    Set additionalInfo = method("aditionalInfo")
    Response.Write additionalInfo("voucherCode")
Next
%>