ABAP Get Request ve Json Deserialize

Merhabalar ,

Aşağıdaki kodlar ile bir rest servise “GET” isteğiyle dönen json tipi verinin deserialize edilişini ve bir structure içerisine eklenmesini sağlayabilirsiniz.

 

function ztp_json_deserialize.
*”———————————————————————-
*”*”Local Interface:
*”———————————————————————-

  data: lv_response type string,
        lv_url      type string,
        lt_table    type standard table of ztp_json_st with header line,  “Must be json data type
        lo_client   type ref to if_http_client.

  lv_url = ‘http://apiadress/api/controller/methodname/parameters.

* Create client.
  call method cl_http_client=>create_by_url(
    exporting
      url                = lv_url
    importing
      client             = lo_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4 ).

* Set requests method. Ex ‘GET’ or ‘POST’.
  lo_client->request->set_header_field( name  = ‘~request_method’
                                          value = ‘GET’ ).

* Send requests.
  call method lo_client->send(
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      others                     = 4 ).

  call method lo_client->receive(
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      others                     = 4 ).

* Get response.
  lv_response = lo_client->response->get_cdata( ).

* Close client.
  lo_client->close( ).

  if ( lv_response(1) ne ‘[‘).
    concatenate ‘[‘ lv_response ‘]’ into lv_response.
  endif.

  call method zcl_ui2_cl_json=>deserialize
    exporting
      json        = lv_response
      pretty_name = zcl_ui2_cl_json=>pretty_modecamel_case
    changing
      data        = lt_table[].

You may also like...

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

This site uses Akismet to reduce spam. Learn how your comment data is processed.