ABAP 格式与JSON和XML格式互转

一、前言

目前项目中很多地方会用到将ABAP的格式转换为JSON或者XML格式,下面介绍几种方法:

對transformation的方式生成XML文件,方法有多種:

  • call transformation 
  • cl_proxy_xml_transform=>abap_to_xml_xstring的方式:
  • /ui2/cl_json
  • CL_TREX_JSON_SERIALIZER与CL_TREX_JSON_DESERIALIZER

二、call transformation

  DATA: lo_writer TYPE REF TO cl_sxml_string_writer.

  lo_writer = cl_sxml_string_writer=>create(
      type                     = if_sxml=>co_xt_xml10
      encoding                 = `UTF-8` ).
  CALL TRANSFORMATION id
  SOURCE spfli = lt_itab
         sflight = lt_itab2
  RESULT XML lo_writer.
  lv_xml = lo_writer->get_output( ).

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = lv_xml
    IMPORTING
      output_length = lv_size
    TABLES
      binary_tab    = lt_xml.

若是需要json格式,則可以將type的參數改為IF_SXML=>CO_XT_BINARY,若目標內表中存在非扁平結構,需要在SE11中提前定義好結構,若需要像PI結構一樣可以控制initial字段,內表在最終的XML或者文件中可見,則需要在SE80中定義結構Enterprise Services中定義。

三、cl_proxy_xml_transform=>abap_to_xml_xstring

  cl_proxy_xml_transform=>abap_to_xml_xstring(
    EXPORTING
      abap_data    = is_itab3
      ddic_type    = 'ZLIAN_TEST_XML' " Name of Dictionary Type
*      ext_xml      = ABAP_FALSE
*      xml_header   = 'no'
*      root_element =
*      svar_name    =     " Proxy Generation: Name of an Object in R/3
    RECEIVING
      xml          = lv_xml ).
    CATCH cx_proxy_fault.    " Proxy Fault
    CATCH cx_transformation_error.    " General Error When Performing CALL TRANSFORMATION

注:若是多層結構,兩種方式需要建立的結構不一樣,cl_proxy_xml_transform=>abap_to_xml_xstring不會產生最內層的tablel_line,最外層的name也不會產生;也不會帶出空字段,call transformation則都相反。

四、/ui2/cl_json

JSON转 ABAP结构

TYPES: BEGIN OF t_record,
         key1 TYPE string,
         key2 TYPE string,
       END OF t_record.

DATA: json TYPE string.
DATA: record TYPE t_record.

json = '{"key1": "VALUE1",  "key2": "VALUE2"}'.

/ui2/cl_json=>deserialize( EXPORTING json = json CHANGING data = record ).
IF sy-subrc = 0. ENDIF.

ABAP结构转JSON

json = /ui2/cl_json=>serialize( data = record compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

当然SAP还是提供了其它类来进行JSON格式的转换。

但是这种JSON的方法出来的名字会直接保存为小写,意味着KEY1/KEY2在JSON中永远都是key1/key2

{"key1":"ValuE1","key2":"VALUE2"}

怎样才可以做到{"KEy1":"ValuE1","key2":"VALUE2"}

这时候需要做到驼峰美化

DATA: BEGIN OF record,
         _K_EY1 TYPE string VALUE 'ValuE1',
         key2 TYPE string VALUE 'VALUE2',
       END OF record.
DATA: json TYPE string.

json = /ui2/cl_json=>serialize( data = record compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

五、CL_TREX_JSON_SERIALIZER与CL_TREX_JSON_DESERIALIZER

  • CL_TREX_JSON_SERIALIZER :将 ABAP 内表转换为 json 格式
  • CL_TREX_JSON_DESERIALIZER:json 转换为 ABAP 内表

这两个类有一点点问题,序列化生成的格式 key 没有引号,可能导致外部解析的失败。比如

[
    {
        id: "001",
        name: "Stone"
    },
    {
        id: "002",
        name: "Brown"
    }
]

 而我们需要的是下面的格式:

[
    {
        "id": "001",
        "name": "Stone"
    },
    {
        "id": "002",
        "name": "Brown"
    }
]

所以需要对两个类进行改造,以符合自己的需求。首先我们使用事务码 SE24 将 CL_TREX_JSON_SERIALIZER 类拷贝一个新类,另存为 ZCL_TREX_JSON_SERIALIZER,然后将 RECURSE() 方法做两点改变:

填加语句:

注释掉 CONCATENATE <abapcomp>-name c_colon INTO l_value 语句,改写为下面的语句。就是在 <abapcomp>-name 两边加上双引号 :

 

完整的 RECURSE() 方法代码如下:

METHOD recurse.

  DATA:
  l_type  TYPE c ,
  l_comps TYPE i ,
  l_lines TYPE i ,
  l_index TYPE i ,
  l_value TYPE string .
  FIELD-SYMBOLS:
    <itab> TYPE ANY TABLE ,
    <comp> TYPE ANY .

  DESCRIBE FIELD data TYPE l_type COMPONENTS l_comps .

  IF l_type = cl_abap_typedescr=>typekind_table .
*   itab -> array
    APPEND '[' TO me->fragments .
    ASSIGN data TO <itab> .
    l_lines = LINES( <itab> ) .
    LOOP AT <itab> ASSIGNING <comp> .
      ADD 1 TO l_index .
      recurse( <comp> ) .
      IF l_index < l_lines .
        APPEND c_comma TO me->fragments .
      ENDIF .
    ENDLOOP .
    APPEND ']' TO fragments .
  ELSE .
    IF l_comps IS INITIAL .
*     field -> scalar
*     todo: format
      l_value = data .
      REPLACE ALL OCCURRENCES OF '\' IN l_value WITH '\\' .
      REPLACE ALL OCCURRENCES OF '''' IN l_value WITH '\''' .
      REPLACE ALL OCCURRENCES OF '"' IN l_value WITH '\"' .
      REPLACE ALL OCCURRENCES OF '&' IN l_value WITH '\&' .
      REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf IN l_value WITH '\r\n' .
      REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>newline IN l_value WITH '\n' .
      REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN l_value WITH '\t' .
      REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>backspace IN l_value WITH '\b' .
      REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>form_feed IN l_value WITH '\f' .
      CONDENSE l_value.
      CONCATENATE '"' l_value '"' INTO l_value .
      APPEND l_value TO me->fragments .
    ELSE .
*     structure -> object
      DATA l_typedescr TYPE REF TO cl_abap_structdescr .
      FIELD-SYMBOLS <abapcomp> TYPE abap_compdescr .

      APPEND '{' TO me->fragments .
      l_typedescr ?= cl_abap_typedescr=>describe_by_data( data ) .
      LOOP AT l_typedescr->components ASSIGNING <abapcomp> .
        l_index = sy-tabix .
*        CONCATENATE <abapcomp>-name c_colon INTO l_value .
        CONCATENATE '"' <abapcomp>-name '"' c_colon INTO l_value.
        TRANSLATE l_value TO LOWER CASE .
        APPEND l_value TO me->fragments .
        ASSIGN COMPONENT <abapcomp>-name OF STRUCTURE data TO <comp> .
        recurse( <comp> ) .
        IF l_index < l_comps .
          APPEND c_comma TO me->fragments .
        ENDIF .
      ENDLOOP .
      APPEND '}' TO me->fragments .
    ENDIF .
  ENDIF .

ENDMETHOD.

 相对应地, 将 CL_TREX_JSON_DESERIALIZER 拷贝成为 ZCL_TREX_JSON_DESERIALIZER,并且对 deserialize_object 方法做两点变更 (网上有一个做好的图片,我就直接贴图了):

完成后,deserialize_object 方法的完整代码如下:

METHOD deserialize_object.
  DATA:
  l_node_type TYPE REF TO cl_abap_typedescr ,
  l_ref TYPE REF TO object .

  ADD 1 TO offset . "skip {

  l_node_type = cl_abap_typedescr=>describe_by_data( node ) .

* prepare for dynamic access
  CASE l_node_type->kind .
    WHEN cl_abap_typedescr=>kind_ref .
      l_ref = node .
    WHEN cl_abap_typedescr=>kind_struct .

    WHEN OTHERS .
      RAISE EXCEPTION TYPE cx_trex_serialization .
  ENDCASE .

  DATA:
    l_done TYPE abap_bool ,
    l_len TYPE i ,
    l_name TYPE string .

* handle each component
  WHILE l_done = abap_false .
    "find next key
    FIND REGEX '"(\w+)\s*":' IN SECTION OFFSET offset OF json
      MATCH OFFSET offset MATCH LENGTH l_len
      SUBMATCHES l_name .
    IF sy-subrc <> 0 .
      RAISE EXCEPTION TYPE cx_trex_serialization .
    ENDIF .
    ADD l_len TO offset .

    FIELD-SYMBOLS <comp> TYPE ANY .

*   dynamic binding to component
    TRANSLATE l_name TO UPPER CASE .
    CASE l_node_type->kind .
      WHEN cl_abap_typedescr=>kind_ref .
        ASSIGN l_ref->(l_name) TO <comp> .
      WHEN cl_abap_typedescr=>kind_struct .
        ASSIGN COMPONENT l_name OF STRUCTURE node TO <comp> .
        IF sy-subrc <> 0.
          CONTINUE.
        ENDIF.

      WHEN OTHERS .
        RAISE EXCEPTION TYPE cx_trex_serialization .
    ENDCASE .

    DATA:
      l_comp_type TYPE REF TO cl_abap_typedescr ,
      l_ref_type TYPE REF TO cl_abap_refdescr .

*   check component type
    l_comp_type = cl_abap_typedescr=>describe_by_data( <comp> ) .
    CASE l_comp_type->kind .
*     create instance if it's an oref
      WHEN cl_abap_typedescr=>kind_ref .
        l_ref_type ?= l_comp_type .
        l_comp_type = l_ref_type->get_referenced_type( ) .
        CREATE OBJECT <comp> TYPE (l_comp_type->absolute_name) .
    ENDCASE .

*   deserialize current component
    deserialize_node(
      EXPORTING
        json = json
      CHANGING
        offset = offset
        node = <comp> ) .

    FIND REGEX ',|\}' IN SECTION OFFSET offset OF json MATCH OFFSET offset .
    IF sy-subrc <> 0 .
      RAISE EXCEPTION TYPE cx_trex_serialization .
    ENDIF .

    IF json+offset(1) = '}' .
      l_done = abap_true .
    ENDIF .
    ADD 1 TO offset .
  ENDWHILE .

ENDMETHOD.

 示例

调用 ZCL_TREX_JSON_SERIALIZER 实现序列化以及调用 ZCL_TREX_JSON_DESERIALIZER 实现反序列化的代码如下:

REPORT  zabap_json .

DATA: serializer TYPE REF TO zcl_trex_json_serializer,
      lv_json TYPE string.

DATA: BEGIN OF ls_json,
  id TYPE string,
  name TYPE string,
  END OF ls_json.

DATA: lt_json LIKE STANDARD TABLE OF ls_json.

CLEAR ls_json.
ls_json-id = '001'.
ls_json-name = 'Stone'.

APPEND ls_json TO lt_json.

CLEAR ls_json.
ls_json-id = '002'.
ls_json-name = 'Brown'.

APPEND ls_json TO lt_json.

CREATE OBJECT serializer
    EXPORTING DATA = lt_json[].
CALL METHOD serializer->serialize( ).
lv_json = serializer->get_data( ).

WRITE / lv_json.

* Deserialze

WRITE /.
WRITE: / 'Deseriaze json string to internal table: '.

DATA: deserializer TYPE REF TO zcl_trex_json_deserializer.
CLEAR lt_json.

CREATE OBJECT deserializer.
CALL METHOD deserializer->deserialize(
  EXPORTING json = lv_json
  IMPORTING abap = lt_json[] ).

LOOP AT lt_json INTO ls_json.
  WRITE : / ls_json-id, ls_json-name.
ENDLOOP.

生成XML文件ABAP程序 REPORT zams_xml_dom_create . TYPE-POOLS: ixml. CLASS cl_ixml DEFINITION LOAD. TYPES: BEGIN OF xml_line, data(256) TYPE x, END OF xml_line. DATA: l_ixml TYPE REF TO if_ixml, l_streamfactory TYPE REF TO if_ixml_stream_factory, l_ostream TYPE REF TO if_ixml_ostream, l_renderer TYPE REF TO if_ixml_renderer, l_document TYPE REF TO if_ixml_document. DATA:l_element_ArchiveBatch TYPE REF TO if_ixml_element, l_element_BatchNumber TYPE REF TO if_ixml_element, l_element_TotalFiles TYPE REF TO if_ixml_element, l_element_TotalVolumes TYPE REF TO if_ixml_element, l_element_TotalPieces TYPE REF TO if_ixml_element, l_element_Entries TYPE REF TO if_ixml_element, l_element_Entry TYPE REF TO if_ixml_element, l_element_RecordsCombiningType TYPE REF TO if_ixml_element, l_element_FilePath TYPE REF TO if_ixml_element, l_element_SourceFileName TYPE REF TO if_ixml_element, l_element_ClassificationCode TYPE REF TO if_ixml_element, l_element_ElectronicRecordCode TYPE REF TO if_ixml_element, l_value TYPE string, l_name type string. DATA: l_xml_table TYPE TABLE OF xml_line, l_xml_size TYPE i, l_rc TYPE i. START-OF-SELECTION. *Creating a ixml factory l_ixml = cl_ixml=>create( ). *Creating the dom object model l_document = l_ixml->create_document( ). *Fill root node with value ArchiveBatch l_element_ArchiveBatch = l_document->create_simple_element( name = 'ArchiveBatch' parent = l_document ). * clear l_value. * l_value = '"http://www.w3.org/2001/XMLSchema-instance"'. * l_rc = l_element_ArchiveBatch->set_attribute( name = 'xmlns: *xsi' * value = l_value ). * * clear l_value. * concatenate '"D:\dev\vs\projects\AMS\DotNet\Sinopec.Ams.' * 'Integration\bin\Debug\config\AmsIntegration.xsd"' * into l_value. * l_name = 'xsi:noNamespaceSchemaLocation'. * l_rc = l_element_ArchiveBatch->set_attribute( name = l_name *value = l_value ). *BatchNumber l_element_BatchNumber = l_document->create_simple_element( name = 'BatchNumber' value = '20100609001' parent = l_element_ArchiveBatch ). *TotalFiles l_element_TotalFiles = l_document->create_simple_element( name = 'TotalFiles' value = '5' parent = l_element_ArchiveBatch ). *TotalVolumes l_element_TotalVolumes = l_document->create_simple_element( name = 'TotalVolumes' value = '3' parent = l_element_ArchiveBatch ). *TotalPieces l_element_TotalPieces = l_document->create_simple_element( name = 'TotalPieces' value = '5' parent = l_element_ArchiveBatch ). *Entries l_element_Entries = l_document->create_simple_element( name = 'Entries' parent = l_element_ArchiveBatch ). *Entry1 l_element_Entry = l_document->create_simple_element( name = 'Entry' parent = l_element_Entries ). *Entry1下面的子项 *RecordsCombiningType l_element_RecordsCombiningType = l_document->create_simple_element( name = 'RecordsCombiningType' value = 'Volume' parent = l_element_Entry ). *FilePath l_element_FilePath = l_document->create_simple_element( name = 'FilePath' value = '全宗001/目录001/案卷001/档案一.docx' parent = l_element_Entry ). *SourceFileName l_element_SourceFileName = l_document->create_simple_element( name = 'SourceFileName' value = '档案一' parent = l_element_Entry ). *ClassificationCode l_element_ClassificationCode = l_document->create_simple_element( name = 'ClassificationCode' value = 'wenshu' parent = l_element_Entry ). *ElectronicRecordCode l_element_ElectronicRecordCode = l_document->create_simple_element( name = 'ElectronicRecordCode' value = 'BJSH-001' parent = l_element_Entry ). *Entry1 * Creating a stream factory l_streamfactory = l_ixml->create_stream_factory( ). * Connect internal XML table to stream factory l_ostream = l_streamfactory->create_ostream_itable( table = l_xml_table ). * Rendering the document l_renderer = l_ixml->create_renderer( ostream = l_ostream document = l_document ). l_rc = l_renderer->render( ). * Saving the XML document l_xml_size = l_ostream->get_num_written_raw( ). CALL METHOD cl_gui_frontend_services=>gui_download EXPORTING bin_filesize = l_xml_size filename = 'c:\temp\metas.xml' filetype = 'BIN' CHANGING data_tab = l_xml_table EXCEPTIONS OTHERS = 24. IF sy-subrc 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值