1

I have this JSON and no matter what I try I can´t get any values from it. I don´t know what Im missing?

This is the json string.

 {
        "groups": [{
            "name": "Credit Card",
            "types": ["mc", "visa", "amex"]
        }],
        "paymentMethods": [{
            "brands": ["mc", "visa", "amex"],
            "details": [{
                "key": "encryptedCardNumber",
                "type": "cardToken"
            }, {
                "key": "encryptedSecurityCode",
                "type": "cardToken"
            }, {
                "key": "encryptedExpiryMonth",
                "type": "cardToken"
            }, {
                "key": "encryptedExpiryYear",
                "type": "cardToken"
            }, {
                "key": "holderName",
                "optional": true,
                "type": "text"
            }],
            "name": "Card",
            "type": "scheme"
        }, {
            "name": "Få först. Betala sen med Klarna.",
            "supportsRecurring": true,
            "type": "klarna"
        }, {
            "name": "Trustly",
            "supportsRecurring": true,
            "type": "trustly"
        }, {
            "name": "Swish",
            "supportsRecurring": true,
            "type": "swish"
        }, {
            "name": "Paysafecard",
            "supportsRecurring": true,
            "type": "paysafecard"
        }, {
            "name": "Dela upp med Klarna.",
            "supportsRecurring": true,
            "type": "klarna_account"
        }, {
            "name": "Pay now with Klarna.",
            "supportsRecurring": true,
            "type": "klarna_paynow"
        }]
    }

And then I try to get one single value(the "Credit Card") and also loop through all the paymetsMethods.details.key values and then loop all the last "name".

<!--#include file="aspJSON1.17.asp" -->
        jsonstring = CStr(objXmlHttp.ResponseText)
        'response.write "---"&jsonstring &"---"
        Set oJSON = New aspJSON
        oJSON.loadJSON(jsonstring)
        
        groupsname=oJSON.data("groups").item("name")
        response.write groupsname
        
        For Each x In oJSON.data("paymentMethods").item("details")
        paymentMethods=oJSON.data("paymentMethods").item("details").item("key")
        response.write paymentMethods
        NEXT
        
        For Each x In oJSON.data("name")
        thename=oJSON.data.item("name")
        response.write thename
        NEXT

Im clearly missing something, but what?

user692942
  • 16,398
  • 7
  • 76
  • 175
Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
  • I just found your question looking for newly created tags and wonder why we need two new asp-tags, considering [there are already about 120 of them](https://stackoverflow.com/tags?page=117&tab=name). Is it about asp.net? – jps Oct 04 '20 at 08:15
  • Problem with the tag [tag:aspjson] is it's too ambiguous as there are other libraries called [aspJSON](https://github.com/rcdmk/aspJSON) as opposed to [this one](https://www.aspjson.com/) which I think the OP is using. – user692942 Oct 04 '20 at 13:17
  • What happens, does it print nothing does it error... what? – user692942 Oct 04 '20 at 13:21
  • You need to look at the duplicate the issue here is you need to first loop through `paymentMethods` before you can then get at each `details` collection. The code needs a nested loop. – user692942 Oct 04 '20 at 13:23

1 Answers1

0

When parsing JSON if you come across a collection (i.e. an Array) you have to iterate through it before trying to retrieve values deeper in the JSON structure.

In this case, to get to the key property you have to iterate through two collections.

  1. paymentMethods
  2. details

So this For loop;

For Each x In oJSON.data("paymentMethods").item("details")
paymentMethods=oJSON.data("paymentMethods").item("details").item("key")
response.write paymentMethods
NEXT

Should look more like;

Dim paymentMethod, paymentMethodsItem, details, detailsItem
For Each paymentMethodsItem In oJSON.data("paymentMethods")
    Set paymentMethod = oJSON.data("paymentMethods").item(paymentMethodsItem)
    For Each detailsItem In paymentMethod.data("details")
        Set details = paymentMethod.data("details").item(detailsItem)
        Call Response.Write(details.item("key"))
    Next
Next

#Code provided untested


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175
  • Did this answer help? If so, would you consider leaving an up-vote and maybe even [accepting the answer](https://stackoverflow.com/help/someone-answers)? – user692942 May 13 '21 at 01:05