0

tenho conhecimento básico de JS, gosto de trabalhar com jQuery que facilita bastante.

Eu tenho vários dados em Json e gostaria de criar algumas variáveis com resultado de algumas linhas do Json, segue um Json de exemplo que tenho:

Array
(
[body] => stdClass Object
    (
        [id] => xxxxxx
        [mode] => me2
        [created_by] => receiver
        [order_id] => xxxxxxxxxxx
        [order_cost] => 69.9
        [site_id] => MLB
        [status] => ready_to_ship
        [substatus] => ready_to_print
        [status_history] => stdClass Object
            (
                [date_cancelled] => 
                [date_delivered] => 
                [date_first_visit] => 
                [date_handling] => 2017-07-14T04:41:32.000-04:00
                [date_not_delivered] => 
                [date_ready_to_ship] => 2017-07-14T04:41:32.000-04:00
                [date_shipped] => 
                [date_returned] => 
            )

        [date_created] => 2017-07-14T04:40:47.000-04:00
        [last_updated] => 2017-07-14T04:41:32.000-04:00
        [tracking_number] => xxxxxxxxxxx
        [tracking_method] => PAC
        [service_id] => 21
        [carrier_info] => 
        [sender_id] => xxxxxxxxx
        [sender_address] => stdClass Object
            (
                [id] => xxxxxxxx
                [address_line] => xxxxxxxxxx
                [street_name] => xxxxxxxxx
                [street_number] => 2
                [comment] => Lj 1
                [zip_code] => xxxxxxxx
                [city] => stdClass Object
                    (
                        [id] => xxxxxxxxxx
                        [name] => xxxxxxxx
                    )

                [state] => stdClass Object
                    (
                        [id] => BR-DF
                        [name] => Distrito Federal
                    )

                [country] => stdClass Object
                    (
                        [id] => BR
                        [name] => Brasil
                    )

                [neighborhood] => stdClass Object
                    (
                        [id] => 
                        [name] => Taguatinga Norte (Taguatinga)
                    )

                [municipality] => stdClass Object
                    (
                        [id] => 
                        [name] => 
                    )

                [agency] => 
                [types] => Array
                    (
                        [0] => billing
                        [1] => default_buying_address
                        [2] => default_selling_address
                        [3] => shipping
                    )

                [latitude] => -15.83
                [longitude] => -48.06
                [geolocation_type] => RANGE_INTERPOLATED
            )

        [receiver_id] => 81184358
        [receiver_address] => stdClass Object
            (
                [id] => xxxxxxxxx
                [address_line] => xxxxxxxxxxxx
                [street_name] => Cinco de julho
                [street_number] => xxx
                [comment] => xxxxxxxxxxx
                [zip_code] => xxxxxxxxxxxx
                [city] => stdClass Object
                    (
                        [id] => BR-RJ-05
                        [name] => Niterói
                    )

                [state] => stdClass Object
                    (
                        [id] => BR-RJ
                        [name] => Rio de Janeiro
                    )

                [country] => stdClass Object
                    (
                        [id] => BR
                        [name] => Brasil
                    )

                [neighborhood] => stdClass Object
                    (
                        [id] => 
                        [name] => Icaraí
                    )

                [municipality] => stdClass Object
                    (
                        [id] => 
                        [name] => 
                    )

                [agency] => 
                [types] => 
                [latitude] => -22.9
                [longitude] => -43.1
                [geolocation_type] => RANGE_INTERPOLATED
                [receiver_name] => xxxxxxxxx
                [receiver_phone] => xxxxxxxxxxx
            )

        [shipping_items] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => xxxxxxxxxxx
                        [description] => DESCRIÇAO DO PRODUTO
                        [quantity] => 1
                        [dimensions] => 8.0x15.0x18.0,132.0
                    )

            )

        [shipping_option] => stdClass Object
            (
                [id] => 27305403
                [shipping_method_id] => 100009
                [name] => Normal
                [currency_id] => BRL
                [cost] => 20.9
                [list_cost] => 20.9
                [estimated_handling_limit] => stdClass Object
                    (
                        [date] => 2017-07-17T00:00:00.000-03:00
                    )

                [estimated_delivery_final] => stdClass Object
                    (
                        [date] => 2017-08-25T00:00:00.000-03:00
                    )

                [estimated_delivery_limit] => stdClass Object
                    (
                        [date] => 2017-08-11T00:00:00.000-03:00
                    )

                [estimated_delivery_extended] => stdClass Object
                    (
                        [date] => 2017-08-02T00:00:00.000-03:00
                    )

                [estimated_delivery_time] => stdClass Object
                    (
                        [type] => known_frame
                        [date] => 2017-07-25T00:00:00.000-03:00
                        [unit] => hour
                        [offset] => stdClass Object
                            (
                                [date] => 2017-07-28T00:00:00.000-03:00
                                [shipping] => 72
                            )

                        [time_frame] => stdClass Object
                            (
                                [from] => 
                                [to] => 
                            )

                        [pay_before] => 
                        [shipping] => 144
                        [handling] => 24
                    )

            )

        [comments] => 
        [date_first_printed] => 
        [market_place] => MELI
        [return_details] => 
        [tags] => Array
            (
            )

        [type] => forward
        [application_id] => 
        [return_tracking_number] => 
        [cost_components] => stdClass Object
            (
                [special_discount] => 0
                [loyal_discount] => 0
                [compensation] => 0
            )

        [picking_type] => drop_off
        [logistic_type] => drop_off
        [substatus_history] => Array
            (
            )

        [delay] => Array
            (
            )

    )

[httpCode] => 200
)
Array
(
[body] => stdClass Object
    (
        [id] => 1423024865
        [date_created] => 2017-07-14T04:40:47.000-04:00
        [date_closed] => 2017-07-14T04:41:28.000-04:00
        [last_updated] => 2017-07-14T04:41:28.000-04:00
        [feedback] => stdClass Object
            (
                [sale] => 
                [purchase] => 
            )

        [mediations] => Array
            (
            )

        [comments] => 
        [order_request] => stdClass Object
            (
                [return] => 
                [change] => 
            )

        [fulfilled] => 
        [shipping] => stdClass Object
            (
                [id] => 26279834391
                [site_id] => MLB
                [shipment_type] => shipping
                [mode] => me2
                [shipping_mode] => me2
                [status] => ready_to_ship
                [shipping_items] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => XXXXXXXXXXXX
                                [description] => DESCRIÇÃO DO PRODUTO
                                [quantity] => 1
                                [dimensions] => 8.0x15.0x18.0,132.0
                            )

                    )

                [shipping_option] => stdClass Object
                    (
                        [id] => 27305403
                        [shipping_method_id] => 100009
                        [name] => Normal
                        [currency_id] => BRL
                        [cost] => 20.9
                        [list_cost] => 20.9
                        [estimated_handling_limit] => stdClass Object
                            (
                                [date] => 2017-07-17T00:00:00.000-03:00
                            )

                        [estimated_delivery_final] => stdClass Object
                            (
                                [date] => 2017-08-25T00:00:00.000-03:00
                            )

                        [estimated_delivery_limit] => stdClass Object
                            (
                                [date] => 2017-08-11T00:00:00.000-03:00
                            )

                        [estimated_delivery_extended] => stdClass Object
                            (
                                [date] => 2017-08-02T00:00:00.000-03:00
                            )

                        [estimated_delivery_time] => stdClass Object
                            (
                                [type] => known_frame
                                [date] => 2017-07-25T00:00:00.000-03:00
                                [unit] => hour
                                [offset] => stdClass Object
                                    (
                                        [date] => 2017-07-28T00:00:00.000-03:00
                                        [shipping] => 72
                                    )

                                [time_frame] => stdClass Object
                                    (
                                        [from] => 
                                        [to] => 
                                    )

                                [pay_before] => 
                                [shipping] => 144
                                [handling] => 24
                            )

                    )

                [currency_id] => BRL
                [receiver_address] => stdClass Object
                    (
                        [id] => 11099787
                        [address_line] => XXXXXXXXXXXX
                        [street_name] => Cinco de julho
                        [street_number] => XXX
                        [comment] => XXXXXXXX
                        [zip_code] => XXXXXXXXX
                        [city] => stdClass Object
                            (
                                [id] => BR-RJ-05
                                [name] => Niterói
                            )

                        [state] => stdClass Object
                            (
                                [id] => BR-RJ
                                [name] => Rio de Janeiro
                            )

                        [country] => stdClass Object
                            (
                                [id] => BR
                                [name] => Brasil
                            )

                        [neighborhood] => stdClass Object
                            (
                                [id] => 
                                [name] => Icaraí
                            )

                        [municipality] => stdClass Object
                            (
                                [id] => 
                                [name] => 
                            )

                        [agency] => 
                        [types] => 
                        [latitude] => -22.9
                        [longitude] => -43.1
                        [geolocation_type] => RANGE_INTERPOLATED
                        [receiver_name] => XXXXXXXX
                        [receiver_phone] => XXXXXXXXXXX
                    )

                [sender_address] => stdClass Object
                    (
                        [id] => xxxxxxxxxx
                        [address_line] => xxxxxxx
                        [street_name] => xxxxxx
                        [street_number] => 2
                        [comment] => xxxxxxx
                        [zip_code] => xxxxxxxx
                        [city] => stdClass Object
                            (
                                [id] => xxxxxxxxxx
                                [name] => Brasília
                            )

                        [state] => stdClass Object
                            (
                                [id] => BR-DF
                                [name] => Distrito Federal
                            )

                        [country] => stdClass Object
                            (
                                [id] => BR
                                [name] => Brasil
                            )

                        [neighborhood] => stdClass Object
                            (
                                [id] => 
                                [name] => Taguatinga Norte (Taguatinga)
                            )

                        [municipality] => stdClass Object
                            (
                                [id] => 
                                [name] => 
                            )

                        [agency] => 
                        [types] => Array
                            (
                                [0] => billing
                                [1] => default_buying_address
                                [2] => default_selling_address
                                [3] => shipping
                            )

                        [latitude] => -15.83
                        [longitude] => -48.06
                        [geolocation_type] => RANGE_INTERPOLATED
                    )

                [picking_type] => drop_off
                [logistic_type] => drop_off
                [cost] => 20.9
                [substatus] => ready_to_print
                [date_created] => 2017-07-14T04:40:47.000-04:00
                [date_first_printed] => 
                [service_id] => 21
                [receiver_id] => 81184358
                [sender_id] => 135930319
                [cost_components] => stdClass Object
                    (
                        [special_discount] => 0
                        [loyal_discount] => 0
                        [compensation] => 0
                    )

            )

        [expiration_date] => 2017-08-04T04:41:28.000-04:00
        [status] => paid
        [status_detail] => 
        [order_items] => Array
            (
                [0] => stdClass Object
                    (
                        [item] => stdClass Object
                            (
                                [id] => XXXXXXXXXX
                                [title] => DESCRIÇÃO DO PRODUTO
                                [category_id] => XXXXX
                                [variation_id] => 
                                [seller_custom_field] => 
                                [variation_attributes] => Array
                                    (
                                    )

                                [warranty] => 
                                [condition] => new
                            )

                        [quantity] => 1
                        [unit_price] => 69.9
                        [currency_id] => BRL
                    )

            )

        [currency_id] => BRL
        [buyer] => stdClass Object
            (
                [id] => 81184358
                [nickname] => NICKNAMECOMPRADOR
                [email] => EMAILCOMPRADOR
                [phone] => stdClass Object
                    (
                        [area_code] => 0021
                        [number] => 980558594
                        [extension] => 
                        [verified] => 
                    )

                [alternative_phone] => stdClass Object
                    (
                        [area_code] => XXXX
                        [number] => XXXXXX
                        [extension] => 
                    )

                [first_name] => XX
                [last_name] => XXXXXXXX
                [billing_info] => stdClass Object
                    (
                        [doc_type] => CPF
                        [doc_number] => XXXXXXXX
                    )

            )

        [seller] => stdClass Object
            (
                [id] => 135930319
                [nickname] => XXXXXXX
                [email] => XXXXXXXXXXXXXX
                [phone] => stdClass Object
                    (
                        [area_code] => 
                        [extension] => 
                        [number] => XXXXXXX
                        [verified] => 
                    )

                [alternative_phone] => stdClass Object
                    (
                        [area_code] => 
                        [extension] => 
                        [number] => 
                    )

                [first_name] => XXXXX
                [last_name] => XXXXX
            )

        [payments] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => XXXXXX
                        [order_id] => XXXXXXX
                        [payer_id] => XXXXXX
                        [collector] => stdClass Object
                            (
                                [id] => XXXXX
                            )

                        [card_id] => XXXXXXXXX
                        [site_id] => MLB
                        [reason] => DESCRIÇÃO DO PRODUTO
                        [payment_method_id] => master
                        [currency_id] => BRL
                        [installments] => 4
                        [issuer_id] => 24
                        [atm_transfer_reference] => stdClass Object
                            (
                                [company_id] => 
                                [transaction_id] => 
                            )

                        [coupon_id] => 
                        [activation_uri] => 
                        [operation_type] => regular_payment
                        [payment_type] => credit_card
                        [available_actions] => Array
                            (
                                [0] => refund
                            )

                        [status] => approved
                        [status_code] => 0000
                        [status_detail] => accredited
                        [transaction_amount] => 69.9
                        [shipping_cost] => 20.9
                        [coupon_amount] => 0
                        [overpaid_amount] => 0
                        [total_paid_amount] => 97.78
                        [installment_amount] => 24.45
                        [deferred_period] => 
                        [date_approved] => 2017-07-14T04:41:24.000-04:00
                        [authorization_code] => T55196
                        [transaction_order_id] => 3153246762
                        [date_created] => 2017-07-14T04:40:54.000-04:00
                        [date_last_modified] => 2017-07-14T04:41:24.000-04:00
                    )

            )

        [coupon] => stdClass Object
            (
                [id] => 
                [amount] => 0
            )

        [tags] => Array
            (
                [0] => not_delivered
                [1] => paid
            )

        [total_amount] => 69.9
        [total_amount_with_shipping] => 90.8
        [paid_amount] => 90.8
    )

[httpCode] => 200
)

Eu preciso criar variáveis para tracking_number, buyer-nickname, buyer-email e shipping_items-description.

podem me dar um pontapé inicial de como faço?

Obrigado!

2
  • Não entendi muito bem Daniel, esse código está enorme, não tem como sintetizar?
    – BrTkCa
    Commented 14/07/2017 às 15:14
  • Podes explicar como queres usar essas variáveis?
    – Sergio
    Commented 14/07/2017 às 15:34

2 Respostas 2

1

O JQuery contém um método para manipulação de JSON chamado: JQuery.parseJSON (). Para utilizá-lo é bem simples:

var pessoas = jQuery.parseJSON( '{ "name": "John" }' );

Segue documentação: JQuery.parseJSON ()

Depois, para percorrer o objeto poderá fazer assim:

pessoas.forEach(function(pessoa, index) {
    if( pessoa.name === "John" ) {
        //executa regra
    }
});
-1

Consegui. Como tinham muitas informações no Json que eu não estava conseguindo tratar no Javascript, filtrei primeiro no PHP para me retornar somente os dados que eu queria:

{"tracking_number": "xxxxxxxxxxx", "description": "DESCRIÇÃO DO PRODUTO", "nickname": "NickComprador", "email": "EmailComprador"}

e no Javascript:

url_get_tn = 'https://www.meusite.com/webtracker/get_tn.php?order=' + 
order_number;
product_title="";client_email="";username="";tracking_number=""; //define as variaveis vazias para não dar erro no console caso não consiga capturar os dados
$.getJSON( url_get_tn, function(result) {
    product_title = result.description;
    client_email = result.email;
    username =  result.nickname;
    tracking_number = result.tracking_number;
    if (tracking_number.length == 13){
        console.log('Tracking Number obtido da API: ' + tracking_number);
        cadastrar_webtracker();
    }
});

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .