[ABAP] JSON -> Struktur

Variante 1 (/ui2/cl_json)

* ABAP Zieldatentyp
TYPES: BEGIN OF ty_abap,
         param1 TYPE char64,
         param2 TYPE char64,
       END OF ty_abap.

* JSON-Quelldaten
DATA: lv_json_response TYPE string VALUE '{"param1":123,"param2":321}'.
DATA: ls_abap TYPE ty_abap.

TRY.
* JSON->ABAP
    /ui2/cl_json=>deserialize( EXPORTING json        = lv_json_response
                                         pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                               CHANGING  data        = ls_abap ).

    cl_demo_output=>write_data( lv_json_response ).
    cl_demo_output=>write_data( ls_abap ).
    cl_demo_output=>display( ).
  CATCH cx_root INTO DATA(e_txt).
    MESSAGE e_txt->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

Variante 2 (cl_fdt_json)

* ABAP Zieldatentyp
TYPES: BEGIN OF ty_abap,
         success TYPE string,
         msg     TYPE string,
         matnr   TYPE matnr,
       END OF ty_abap.

* JSON-Quelldaten
DATA: lv_json_response TYPE string VALUE '{"success":true,"msg":"Ok.","matnr":0000001234}'.
DATA: ls_abap TYPE ty_abap.

cl_fdt_json=>json_to_data( EXPORTING iv_json = lv_json_response
                           CHANGING  ca_data = ls_abap ).

cl_demo_output=>write_data( lv_json_response ).
cl_demo_output=>write_data( ls_abap ).
cl_demo_output=>display( ).

VAriante 3 (cl_clb_parse_json)

* ABAP Zieldatentyp
TYPES: BEGIN OF ty_abap,
         success TYPE string,
         msg     TYPE string,
         matnr   TYPE matnr,
       END OF ty_abap.

TRY.
* JSON-Quelldaten
    DATA(lv_json_response) = CONV string( '{"success":"true","msg":"Ok.","matnr":"0000001234"}' ).

* JSON nach XSTRING (UTF-8) konvertieren
    DATA(lvx_string) = cl_abap_codepage=>convert_to( lv_json_response ).
* XSTRING nach string wandeln
    DATA(lv_utf8) = cl_clb_tools=>xstring_to_string( lvx_string ).

* JSON->ABAP
    DATA(o_json) = NEW cl_clb_parse_json( ).
    DATA: ls_abap TYPE ty_abap.

    o_json->json_to_data( EXPORTING iv_json = lv_utf8
                          CHANGING c_data   = ls_abap ).

    cl_demo_output=>write_data( lv_json_response ).
    cl_demo_output=>write_data( ls_abap ).
    cl_demo_output=>display( ).
  CATCH cx_root INTO DATA(e_txt).
    MESSAGE e_txt->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

[ABAP] string (Text) nach xstring (Bytes) wandeln

Variante 1 (cl_abap_conv_out_ce)

DATA: lv_str TYPE string VALUE 'Teststring.'.
DATA: lv_xstr TYPE xstring.

* string (UTF-8) -> xstring (binary)
DATA(o_conv_w) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
o_conv_w->write( data = lv_str ).
lv_xstr = o_conv_w->get_buffer( ).

WRITE: / lv_xstr.

Variante 2 (SCMS_STRING_TO_XSTRING)

DATA: lv_str TYPE string VALUE 'Teststring.'.
DATA: lv_xstr TYPE xstring.
  
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  EXPORTING
    text   = lv_str
  IMPORTING
    buffer = lv_xstr.

WRITE: / lv_xstr.

Variante 3 (cl_proxy_service)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string (4110 / UTF-8) -> xstring (binary)
DATA(lv_xstr_res) = cl_proxy_service=>cstring2xstring( lv_str ).
WRITE: / lv_xstr_res.

Variante 4 (cl_abap_codepage)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string -> xstring (binary) (UTF-8)
DATA(xstr) = cl_abap_codepage=>convert_to( source   = lv_str
                                           codepage = 'UTF-8' ).
WRITE: / xstr.

Variante 5 (cl_abap_message_digest)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string (4110 / UTF-8) -> xstring (binary)
DATA(lv_xstr) = cl_abap_message_digest=>string_to_xstring( lv_str ).

WRITE: / lv_xstr.

Variante 6 (cl_abap_hmac)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string (4110 / UTF-8) -> xstring (binary)
DATA(lv_xstr) = cl_abap_hmac=>string_to_xstring( lv_str ).

WRITE: / lv_xstr.

Variante 7 (cl_clb_tools)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string -> xstring (binary) (UTF-8)
DATA(lv_xstr) = cl_clb_tools=>string_to_xstring( lv_str ).

WRITE: / lv_xstr.