Difference between revisions of "SAP ABAP JSON"

From SapWiki
Line 38: Line 38:
 
  ENDFORM.                    "decode
 
  ENDFORM.                    "decode
  
 
+
==Ejemplo Serializing JSON==
 +
<nowiki> *&---------------------------------------------------------------------*
 +
*& Report YTEST_JSON3
 +
*&---------------------------------------------------------------------*
 +
*& ejemplo Serializing JSON
 +
*&---------------------------------------------------------------------*
 +
REPORT ytest_json3 LINE-SIZE 260.
 +
 +
DATA l_string TYPE string.
 +
TYPES: BEGIN OF ty_t001,
 +
          bukrs TYPE t001-bukrs,
 +
          butxt TYPE t001-butxt,
 +
          ort01 TYPE t001-ort01,
 +
          land1 TYPE t001-land1,
 +
          waers TYPE t001-waers,
 +
          spras TYPE t001-spras,
 +
        END OF ty_t001.
 +
DATA lt_data TYPE TABLE OF ty_t001.
 +
 +
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_data FROM t001 UP TO 2 ROWS
 +
                                      where land1 = 'DE'.
 +
 +
CALL METHOD /ui2/cl_json=>serialize
 +
  EXPORTING
 +
    data  = lt_data  " Data to serialize
 +
  RECEIVING
 +
    r_json = l_string.  " JSON string
 +
 +
WRITE:/ l_string.
 +
 +
CALL METHOD /ui2/cl_json=>serialize
 +
  EXPORTING
 +
    data        = lt_data  " Data to serialize
 +
    pretty_name = /ui2/cl_json=>pretty_mode-camel_case
 +
  RECEIVING
 +
    r_json      = l_string.  " JSON string
 +
 +
WRITE:/ l_string.
 +
 +
CALL METHOD /ui2/cl_json=>serialize
 +
  EXPORTING
 +
    data        = lt_data  " Data to serialize
 +
    pretty_name = /ui2/cl_json=>pretty_mode-camel_case
 +
    name        = 'SOCIEDADES'
 +
  RECEIVING
 +
    r_json      = l_string.  " JSON string
 +
 +
WRITE:/ l_string.
 +
 +
 +
TYPES: BEGIN OF ty_data,
 +
          nombre    TYPE string,
 +
          edad      TYPE i,
 +
          nacimiento TYPE d,
 +
          direccion  TYPE string,
 +
          plata(10)  TYPE p DECIMALS 0,
 +
        END OF ty_data.
 +
 +
DATA wa TYPE ty_data.
 +
 +
wa-nombre = 'Deadpool'.
 +
wa-edad = 1000.
 +
wa-nacimiento = '19720915'.
 +
wa-direccion = 'USA'.
 +
wa-plata = 999999.
 +
 +
CALL METHOD /ui2/cl_json=>serialize
 +
  EXPORTING
 +
    data  = wa  " Data to serialize
 +
  RECEIVING
 +
    r_json = l_string.  " JSON string
 +
 +
WRITE:/ l_string.
 +
 +
*####################################################################
 +
TYPES:
 +
  BEGIN OF tp_s_data,
 +
    sschema            TYPE string,
 +
    odatacontext        TYPE string,
 +
    shortened_abap_name TYPE string,
 +
    standard            TYPE string,
 +
  END OF tp_s_data.
 +
 +
DATA: ls_exp    TYPE tp_s_data,
 +
      lt_mapping TYPE /ui2/cl_json=>name_mappings,
 +
      lv_json    TYPE /ui2/cl_json=>json.
 +
 +
lt_mapping = VALUE #( ( abap = `SSCHEMA` json = `$schema` )
 +
                      ( abap = `ODATACONTEXT` json = `@odata.context` )
 +
                      ( abap = `SHORTENED_ABAP_NAME` json = `VeeeeryyyyyLooooongJSONAttrbuuuuuuuuuteeeeeeeeeee` ) ).
 +
 +
ls_exp-sschema = 'sss'.
 +
ls_exp-odatacontext = 'odata'.
 +
ls_exp-shortened_abap_name = 'ssssname'.
 +
ls_exp-standard = 'stan'.
 +
 +
lv_json = /ui2/cl_json=>serialize( data = ls_exp name_mappings = lt_mapping ).
 +
WRITE:/ lv_json.</nowik>
 
[[Category:ABAP]]
 
[[Category:ABAP]]

Revision as of 14:26, 30 March 2020

Ejemplo XSLT JSON

Uso en programas ABAP

FORM crear_json  USING    p_guia TYPE zhr01_dgtl
                          p_uname
                 CHANGING p_string_json TYPE string.
  DATA: json_writer TYPE REF TO cl_sxml_string_writer.
  DATA json TYPE xstring.
  DATA lt_guia TYPE TABLE OF zhr01_dgtl.

  APPEND p_guia TO lt_guia.

  json_writer = cl_sxml_string_writer=>create(
                          type = if_sxml=>co_xt_json ).

  CALL TRANSFORMATION zhcm_guia_dgtl
                    SOURCE root = lt_guia
                           usuario = p_uname
                    RESULT XML json_writer.   "se usa el ejemplo 2 de Ejemplos

  json = json_writer->get_output( ).

*  PERFORM encoding2 CHANGING json p_string_json.
  PERFORM decode USING json
              CHANGING p_string_json.

ENDFORM.                    " CREAR_JSON
FORM decode USING p_xstring
         CHANGING p_string.

  CALL METHOD cl_http_utility=>if_http_utility~decode_utf8
    EXPORTING
      encoded   = p_xstring
    RECEIVING
      unencoded = p_string.

ENDFORM.                    "decode

Ejemplo Serializing JSON

<nowiki> *&---------------------------------------------------------------------*
*& Report YTEST_JSON3
*&---------------------------------------------------------------------*
*& ejemplo Serializing JSON
*&---------------------------------------------------------------------*
REPORT ytest_json3 LINE-SIZE 260.

DATA l_string TYPE string.
TYPES: BEGIN OF ty_t001,
         bukrs TYPE t001-bukrs,
         butxt TYPE t001-butxt,
         ort01 TYPE t001-ort01,
         land1 TYPE t001-land1,
         waers TYPE t001-waers,
         spras TYPE t001-spras,
       END OF ty_t001.
DATA lt_data TYPE TABLE OF ty_t001.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_data FROM t001 UP TO 2 ROWS
                                      where land1 = 'DE'.

CALL METHOD /ui2/cl_json=>serialize
  EXPORTING
    data   = lt_data   " Data to serialize
  RECEIVING
    r_json = l_string.   " JSON string

WRITE:/ l_string.

CALL METHOD /ui2/cl_json=>serialize
  EXPORTING
    data        = lt_data   " Data to serialize
    pretty_name = /ui2/cl_json=>pretty_mode-camel_case
  RECEIVING
    r_json      = l_string.   " JSON string

WRITE:/ l_string.

CALL METHOD /ui2/cl_json=>serialize
  EXPORTING
    data        = lt_data   " Data to serialize
    pretty_name = /ui2/cl_json=>pretty_mode-camel_case
    name        = 'SOCIEDADES'
  RECEIVING
    r_json      = l_string.   " JSON string

WRITE:/ l_string.


TYPES: BEGIN OF ty_data,
         nombre     TYPE string,
         edad       TYPE i,
         nacimiento TYPE d,
         direccion  TYPE string,
         plata(10)  TYPE p DECIMALS 0,
       END OF ty_data.

DATA wa TYPE ty_data.

wa-nombre = 'Deadpool'.
wa-edad = 1000.
wa-nacimiento = '19720915'.
wa-direccion = 'USA'.
wa-plata = 999999.

CALL METHOD /ui2/cl_json=>serialize
  EXPORTING
    data   = wa   " Data to serialize
  RECEIVING
    r_json = l_string.   " JSON string

WRITE:/ l_string.

*####################################################################
TYPES:
  BEGIN OF tp_s_data,
    sschema             TYPE string,
    odatacontext        TYPE string,
    shortened_abap_name TYPE string,
    standard            TYPE string,
  END OF tp_s_data.

DATA: ls_exp     TYPE tp_s_data,
      lt_mapping TYPE /ui2/cl_json=>name_mappings,
      lv_json    TYPE /ui2/cl_json=>json.

lt_mapping = VALUE #( ( abap = `SSCHEMA` json = `$schema` )
                      ( abap = `ODATACONTEXT` json = `@odata.context` )
                      ( abap = `SHORTENED_ABAP_NAME` json = `VeeeeryyyyyLooooongJSONAttrbuuuuuuuuuteeeeeeeeeee` ) ).

ls_exp-sschema = 'sss'.
ls_exp-odatacontext = 'odata'.
ls_exp-shortened_abap_name = 'ssssname'.
ls_exp-standard = 'stan'.

lv_json = /ui2/cl_json=>serialize( data = ls_exp name_mappings = lt_mapping ).
WRITE:/ lv_json.</nowik>