结构转JSON以及JSON文本

Z_STRUC_TO_JSON_JSONTEXT

REPORT  z_struc_to_json_jsontext
              NO STANDARD PAGE HEADING
                MESSAGE-ID pp
                LINE-SIZE 255.
TYPES:BEGIN OF ty_str,
   tdline TYPE string,
END OF ty_str.
DATA lt_str TYPE TABLE OF ty_str WITH HEADER LINE.
DATA  lv_strname TYPE strukname.
DATA: lt_excel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.
DATA: l_data TYPE REF TO data.
DATA: e_json TYPE string,
      e_json_t TYPE string.
FIELD-SYMBOLS: <fs_data> TYPE any.
DATA lv_filename TYPE string.

PARAMETERS:p_file1 TYPE rlgrap-filename.
PARAMETERS:p_file2 TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file1 .
  PERFORM frm_screen_file_value_req USING p_file1."导入路径

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file2 .
  PERFORM frm_screen_file_value_req2 USING p_file2."导入路径

START-OF-SELECTION.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file1
      i_begin_col             = 1
      i_begin_row             = 2
      i_end_col               = 14
      i_end_row               = 9999
    TABLES
      intern                  = lt_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  IF sy-subrc <> 0.

  ENDIF.

  LOOP AT lt_excel.
    CLEAR lv_strname.
    lv_strname = lt_excel-value.
    CONDENSE lv_strname NO-GAPS.
    TRANSLATE lv_strname TO UPPER CASE.
    CALL FUNCTION 'Z_06PM_UTILS_STRU_2_JSON'
      EXPORTING
        strname = lv_strname
      CHANGING
        data    = l_data.
    .

    ASSIGN l_data->* TO <fs_data>.
    CALL FUNCTION 'Z_06PM_ABAP_TO_JSON'
      EXPORTING
        i_data = <fs_data>
      IMPORTING
        e_json = e_json.
    lt_str-tdline = e_json.
    APPEND lt_str.
    CLEAR:lv_filename.

    replace ALL OCCURRENCES OF regex '/' in lv_strname with '-'.
    CONCATENATE p_file2  '\' lv_strname  '.txt' INTO lv_filename.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename = lv_filename
      TABLES
        data_tab = lt_str.
    IF sy-subrc <> 0.
*      implement suitable error handling here
    ENDIF.

    REFRESH lt_str.

    CALL FUNCTION 'Z_06PM_ABAP_TO_JSON'
      EXPORTING
        i_data = <fs_data>
      IMPORTING
        e_json = e_json_t.

    UNASSIGN <fs_data>.

    lt_str-tdline = e_json_t.
    APPEND lt_str.

    CONCATENATE p_file2  '\' lv_strname  '_t'  '.txt' INTO lv_filename.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename = lv_filename
      TABLES
        data_tab = lt_str.
    IF sy-subrc <> 0.
*        implement suitable error handling here
    ENDIF.

    REFRESH lt_str.
  ENDLOOP.

*&---------------------------------------------------------------------
FORM frm_screen_file_value_req USING fname TYPE rlgrap-filename.

  DATA: lt_filetable TYPE filetable,
  lv_rc TYPE i.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = '选择'
      default_extension       = '.XLSX|.XLS'
      file_filter             = 'XLSX (.XLSX)|.XLSX|XLS (.XLS)|.XLS'
    CHANGING
      file_table              = lt_filetable
      rc                      = lv_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.
  IF sy-subrc NE 0.
    MESSAGE '请重新选择文件!' TYPE 'E'.
    RETURN.
  ENDIF.

  READ TABLE lt_filetable INTO fname INDEX 1.

ENDFORM. "frm_screen_file_value_req

*&---------------------------------------------------------------------
*& Form frm_screen_file_value_req2
*&---------------------------------------------------------------------

FORM frm_screen_file_value_req2 USING fname TYPE rlgrap-filename.

  DATA: lv_path TYPE string.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title    = '选择'
      initial_folder  = 'C:'
    CHANGING
      selected_folder = lv_path.

  IF sy-subrc NE 0.
    MESSAGE '请重新选择文件夹!'  TYPE 'e'.
    RETURN.
  ENDIF.

  fname = lv_path.
ENDFORM. "frm_screen_file_value_req2

 

Z_06PM_UTILS_STRU_2_JSON

FUNCTION z_06pm_utils_stru_2_json.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(STRNAME) TYPE  STRUKNAME
*"  CHANGING
*"     VALUE(DATA) TYPE  ANY OPTIONAL
*"----------------------------------------------------------------------
  DATA: lo_structdescr TYPE REF TO cl_abap_structdescr,
        lo_structdescr2 TYPE REF TO cl_abap_structdescr,
        lt_ddic TYPE ddfields, ls_ddic TYPE dfies.
  DATA: data_1 TYPE REF TO data,
        data_2 TYPE REF TO data.
  FIELD-SYMBOLS:<fs_data> TYPE any,
                <comp> TYPE any,
                <comp_1> TYPE any,
                <comp_2> TYPE STANDARD TABLE,
                <comp_3>  TYPE any.
  DATA:lo_tabledescr TYPE REF TO cl_abap_tabledescrDATA: l_header TYPE x030l.

  CREATE DATA data TYPE (strname).
  ASSIGN data->* TO <fs_data>.

  lo_structdescr ?= cl_abap_typedescr=>describe_by_name( strname ).
  lt_ddic = lo_structdescr->get_ddic_field_list).

  LOOP AT lt_ddic INTO ls_ddic.
    CASE ls_ddic-inttype.
      WHEN cl_abap_typedescr=>typekind_struct1.
** do nothing
      WHEN cl_abap_typedescr=>typekind_struct2.
        CALL FUNCTION 'Z_06PM_UTILS_STRU_2_JSON'
          EXPORTING
            strname = ls_ddic-rollname
          CHANGING
            data    = data_1.
        ASSIGN COMPONENT ls_ddic-fieldname OF STRUCTURE <fs_data> TO <comp_1>.
        ASSIGN data_1->* TO <comp>.
        <comp_1> = <comp>.
        UNASSIGN: <comp_1>,<comp> .
      WHEN cl_abap_typedescr=>typekind_table.
        ASSIGN COMPONENT ls_ddic-fieldname OF STRUCTURE <fs_data> TO <comp_2>.
        lo_tabledescr ?= cl_abap_tabledescr=>describe_by_data( <comp_2> ).
        l_header = lo_tabledescr->get_ddic_header).
        CALL FUNCTION 'Z_06PM_UTILS_STRU_2_JSON'
        EXPORTING
            strname = l_header-refname
          CHANGING
            data    = data_2.

        ASSIGN data_2->* TO <comp_3>.
        APPEND <comp_3> TO <comp_2>.
        UNASSIGN: <comp_3>, <comp_2>.
      WHEN OTHERS.
** do nothing
    ENDCASE.
  ENDLOOP.

  UNASSIGN: <fs_data>.

ENDFUNCTION.

Z_06PM_ABAP_TO_JSON

FUNCTION Z_06PM_ABAP_TO_JSON.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_DATA) TYPE  DATA
*"  EXPORTING
*"     VALUE(E_JSON) TYPE  STRING
*"     VALUE(E_FLAG) TYPE  Z06PMEDSTAT
*"----------------------------------------------------------------------
  DATA:cl_serializer   TYPE REF TO zcl_trex_json_serializer.

  CREATE OBJECT cl_serializer
    EXPORTING
      data = i_data.

  cl_serializer->serialize( ).
  e_json = cl_serializer->get_data( ).

ENDFUNCTION.

Z_06PM_JSON_TO_ABAP

FUNCTION z_06pm_json_to_abap.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_JSON) TYPE  STRING
*"  EXPORTING
*"     VALUE(E_DATA) TYPE  DATA
*"     VALUE(E_FLAG) TYPE  Z06PMEDSTAT
*"----------------------------------------------------------------------
  DATA:cl_deserializer TYPE REF TO zcl_trex_json_deserializer.

  DATA:lv_cx_sy_conversion_no_number TYPE REF TO cx_sy_conversion_no_number,
       lv_cx_trex_serialization      TYPE REF TO cx_trex_serialization.

  DATA:lv_message TYPE string,
       lv_msg_typ TYPE c.

  IF i_json IS INITIAL OR i_json '[]'.
    RETURN.
  ENDIF.

  CREATE OBJECT cl_deserializer.

  TRY.
    cl_deserializer->deserialize(
      EXPORTING
        json   = i_json
      IMPORTING
        abap   = e_data ).

    CATCH cx_sy_conversion_no_number INTO lv_cx_sy_conversion_no_number..
      lv_message = lv_cx_sy_conversion_no_number->get_longtext).
      lv_msg_typ 'E'.

    CATCH cx_trex_serialization INTO lv_cx_trex_serialization..
      lv_message = lv_cx_trex_serialization->get_longtext).
      lv_msg_typ 'E'.
  ENDTRY.

  e_flag = lv_msg_typ.
ENDFUNCTION.

 

posted @ 2019-01-15 14:26  蜡哲图  阅读(474)  评论(0编辑  收藏  举报
目录内容