4
I need to get some elements inside a JSON structure with Javascript. I can get unit elements like this:
alert(response.paymentMethods.CREDIT_CARD.options.MASTERCARD.images.SMALL.path);
alert(response.paymentMethods.CREDIT_CARD.options.VISA.images.SMALL.path);
But I need to do a loop to capture ALL these "path" (SMALL) of ALL types of cards. I tried several ways but always return me error or Undefined.
I even followed the steps of this website which would be just what I need, but without success.
Javascript:
PagSeguroDirectPayment.getPaymentMethods({
    success: function(response) {
        //meios de pagamento disponíveis 
    },
    error: function(response) {
        //tratamento do erro 
    },
    complete: function(response) {
        //tratamento comum para todas chamadas 
    }
});
JSON:
{
    "error":false,
    "paymentMethods":{
        "BOLETO":{
            "name":"BOLETO",
            "options":{
                "BOLETO":{
                    "name":"BOLETO",
                    "displayName":"Boleto",
                    "status":"AVAILABLE",
                    "code":202,
                    "images":{
                        "SMALL":{
                            "size":"SMALL",
                            "path":"/public/img/payment-methods-flags/42x20/booklet.png"
                        },
                        "MEDIUM":{
                            "size":"MEDIUM",
                            "path":"/public/img/payment-methods-flags/68x30/booklet.png"
                        }
                    }
                }
            },
            "code":2
        },
        "ONLINE_DEBIT":{
            "name":"ONLINE_DEBIT",
            "options":{
                "BANCO_BRASIL":{
                    "name":"BANCO_BRASIL",
                    "displayName":"Banco do Brasil",
                    "status":"AVAILABLE",
                    "code":304,
                    "images":{
                        "SMALL":{
                            "size":"SMALL",
                            "path":"/public/img/payment-methods-flags/42x20/bb.png"
                        },
                        "MEDIUM":{
                            "size":"MEDIUM",
                            "path":"/public/img/payment-methods-flags/68x30/bb.png"
                        }
                    }
                },
            },
            "code":3
        },
        "CREDIT_CARD":{
            "name":"CREDIT_CARD",
            "options":{
                "MASTERCARD":{
                    "name":"MASTERCARD",
                    "displayName":"MasterCard",
                    "status":"AVAILABLE",
                    "code":102,
                    "images":{
                        "SMALL":{
                            "size":"SMALL",
                            "path":"/public/img/payment-methods-flags/42x20/mastercard.png"
                        },
                        "MEDIUM":{
                            "size":"MEDIUM",
                            "path":"/public/img/payment-methods-flags/68x30/mastercard.png"
                        }
                    }
                },
            },
            "code":1
        }
    }
}
						
This json is poorly formed.
– Marco Antonio Quintal
@Marcoantonioquintal, unfortunately it is like q esta in the manual of pagseguro :( ?
– A.A.F.
I do. I’ll do it here and I’ll send it to you. Just a few minutes.
– Marco Antonio Quintal
have to pick up all small images including Boleto and Banco do Brasil? or just the dredit card?
– Marco Antonio Quintal
CREDIT_CARD only
– A.A.F.
If you want I can pass the rest of the code you test in practice.
– A.A.F.
@A.A.F. to iterate all the cards this JSON needed to have an array for the cards and has no... Can you find the full JSON, at least with more than 1 card? As it is we will be guessing.
– Sergio
@Sergio, Nao sei se isso ajuda mas consigo pegar 2 "path" chamando diretamente: alert(response.paymentMethods.CREDIT_CARD.options.VISA.images.SMALL.path);
alert(response.paymentMethods.CREDIT_CARD.options.MASTERCARD.images.SMALL.path);
– A.A.F.
@A.A.F. Ok, in that case JSON lacks this information and you wouldn’t only know about JSON. Ok, I’ll put an answer for you to test.
– Sergio
@A.A.F: had given a problem in displaying my solution but now it’s ok. forehead there
– Marco Antonio Quintal
@Marcoantonioquintal your solution is working p a card but when comes more than one of the error.
– A.A.F.
@A.A.F. It doesn’t work because the json that pagseguro put in is incomplete. you’re getting the webservice json? Look there. added another card and it worked. passes the full json of webservice.
– Marco Antonio Quintal
@Marcoantonioquintal, you this Seminar. I tested and worked well. Now I only know how to accept two correct answers. Yours and Sergio’s.
– A.A.F.
Heats up no @A.A.F. Only helping is greater joy. Hug
– Marco Antonio Quintal