/ui2/cl_json sınıfı JSON formatındaki dosyayı ayrıştırmamızı sağlar. Bu sınıf karmaşık json yapısını bile sizin için anlamlı bir yapıda sunabiliyor. Aşağıda entegrasyon sırasında kullandığım yapıyı inceleyebilirsiniz.

örnek json verisi :

[ 
  { 
    "id": "5973782bdb9a930533b05cb2",
    "isActive": true,
    "balance": "1.446,35",
    "age": 32,
    "eyeColor": "green",
    "name": "STLOG.NET",
    "gender": "male",
    "company": "STLOG.NET",
    "email": "INFO[AT]STLOG.NET",
    "phone": "+90 (952) 555-5555",
    "friends": [
      { &&
        "id": 0,
        "name": "STLOG.NET - 0"
      },
      {
        "id": 1,
        "name": "STLOG.NET - 1"
      }, &&
      {  &&
        "id": 2,
        "name": "STLOG.NET - 2"
      }
    ],
    "favoriteFruit": "banana"
  }
]

örnek abap kodu

*&---------------------------------------------------------------------*
*& Report Z_STLOG_NET_001
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_stlog_net_001.


DATA(gv_jsonstr) = CONV string(

|[| &&
  '{' &&
    |"id": "5973782bdb9a930533b05cb2",| &&
    |"isActive": true,| &&
    |"balance": "1.446,35",| &&
    |"age": 32,| &&
    |"eyeColor": "green",| &&
    |"name": "STLOG.NET",| &&
    |"gender": "male",| &&
    |"company": "STLOG.NET",| &&
    |"email": "INFO[AT]STLOG.NET",| &&
    |"phone": "+90 (952) 555-5555",| &&
    |"friends": [| &&
      '{' &&
        |"id": 0,| &&
        |"name": "STLOG.NET - 0"| &&
      '},' &&
      '{' &&
        |"id": 1,| &&
        |"name": "STLOG.NET - 1"| &&
      '},' &&
      '{'  &&
        |"id": 2,| &&
        |"name": "STLOG.NET - 2"| &&
      '}' &&
    |],| &&
    |"favoriteFruit": "banana"| &&
  '}' &&
|]|

) .

TYPES : BEGIN OF gty_out ,
          id    TYPE text255,
          name  TYPE text255,
          email TYPE text255,
        END OF gty_out,

        gtty_out TYPE TABLE OF gty_out.

DATA gt_out TYPE gtty_out .


CLASS lcl_main DEFINITION CREATE PUBLIC.

  PUBLIC SECTION .
    CLASS-DATA    : gr_class TYPE REF TO lcl_main .
	
    CLASS-METHODS : createobj RETURNING VALUE(r_result) TYPE REF TO lcl_main .

    METHODS  :

      parse_json_data IMPORTING VALUE(iv_jsondata) TYPE string
                     RETURNING VALUE(rt_out)      LIKE gt_out ,

      get_value      IMPORTING VALUE(is_data)  TYPE any
                               VALUE(iv_fname) TYPE string
                     RETURNING VALUE(rv_val)   TYPE REF TO data
                     .

ENDCLASS .

CLASS lcl_main IMPLEMENTATION .
  METHOD createobj .
    IF gr_class IS INITIAL .
      gr_class = NEW #( ).
    ENDIF.
    r_result = gr_class .
  ENDMETHOD .

  METHOD parse_jso_data .

    DATA dref TYPE REF TO data .
    DATA lv_val TYPE REF TO data .

    FIELD-SYMBOLS :  <fs_data> TYPE ANY TABLE .


    /ui2/cl_json=>deserialize(
      EXPORTING
        json            = iv_jsondata " JSON string
      CHANGING
        data             = dref    " Data to serialize
    ).

    IF dref IS NOT BOUND .
      RETURN .
    ENDIF .

    ASSIGN dref->* TO <fs_data> .

    LOOP AT <fs_data> ASSIGNING FIELD-SYMBOL(<fs_data_s>).
      ASSIGN <fs_data_s>->* TO FIELD-SYMBOL(<fs_any_s>).
      IF <fs_any_s> IS ASSIGNED .

        DATA(s_out) = VALUE gty_out( ) .

        lv_val = get_value( is_data  = <fs_any_s> iv_fname = 'ID' )   .
        IF lv_val IS BOUND .
          ASSIGN lv_val->* TO FIELD-SYMBOL(<any_data>) .
          s_out-id = <any_data> .
        ENDIF.

        lv_val = get_value( is_data  = <fs_any_s> iv_fname = 'NAME' )   .
        IF lv_val IS BOUND .
          ASSIGN lv_val->* TO <any_data> .
          s_out-name = <any_data> .
        ENDIF.

        lv_val = get_value( is_data  = <fs_any_s> iv_fname = 'EMAIL' )   .
        IF lv_val IS BOUND .
          ASSIGN lv_val->* TO <any_data> .
          s_out-email = <any_data> .
        ENDIF.

        IF s_out IS NOT INITIAL.
          APPEND s_out TO rt_out .
          CLEAR s_out .
        ENDIF.
      ENDIF.
    ENDLOOP.

  ENDMETHOD .
  METHOD get_value .
    ASSIGN COMPONENT iv_fname OF STRUCTURE is_data TO FIELD-SYMBOL(<fs_any>) .
    IF <fs_any> IS ASSIGNED .
      TRY .
          ASSIGN <fs_any>->* TO FIELD-SYMBOL(<fs_any_val>).
          rv_val = REF #( <fs_any_val> ) .
        CATCH cx_root.
      ENDTRY.
    ENDIF.
  ENDMETHOD .
ENDCLASS .


START-OF-SELECTION .
  gt_out = lcl_main=>createobj( )->parse_json_data( iv_jsondata = gv_jsonstr ) .
  cl_demo_output=>display_data( value = gt_out ).
2+