分类: Web开发
2015-05-28 15:21:22
语法: CALL TRANSFORMATION id
DATA: lv_em TYPE string,
lv_em1 TYPE string.
DATA: lt_but021 TYPE TABLE OF but021,
lt_but0211 TYPE TABLE OF but021,
ls_but021 TYPE but021.
"数据
lv_em = 'test' .
ls_but021-partner = 'partner1' .
ls_but021-adr_kind = 'kind1' .
ls_but021-addrnumber = 'addr1' .
APPEND ls_but021 TO lt_but021 .
ls_but021-partner = 'partner2' .
ls_but021-adr_kind = 'kind2' .
ls_but021-addrnumber = 'addr2' .
APPEND ls_but021 TO lt_but021 .
" abap 转json
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id
SOURCE content = lv_em
itable = lt_but021
RESULT XML json_writer.
DATA(json) = json_writer->get_output( ).
DATA: lv_json TYPE string .
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = json
im_encoding = 'UTF-8'
IMPORTING
ex_string = lv_json.
" json 转 abap
CALL TRANSFORMATION id
SOURCE XML lv_json
RESULT content = lv_em1
itable = lt_but0211.
方法二
" abap 转json
lv_json = /ui2/cl_json=>serialize( data = ls_data compress = 'X' pretty_name = 'X' name = '' ).
" compress 'X' Skip empty elements
" pretty_name 字段名称格式 'X' 小驼峰,将 _s 转换成S
" pretty_name 字段名称格式 'Y' 增强驼峰,将 _s 转换成S ; __ 转换成 _ ; ___ 转换成 *
" pretty_name 字段名称格式 'L' 全小写
" pretty_name 字段名称格式 ' ' 全大写
" json 转abap
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_json
pretty_name = 'X'
CHANGING
data = ls_data.
方法三
DATA: lr_json_ser TYPE REF TO cl_trex_json_serializer,
lr_json_des TYPE REF TO cl_trex_json_deserializer.
" abap 转json
CREATE OBJECT lr_json_ser
EXPORTING
data = ls_data.
CALL METHOD lr_json_ser->serialize.
CALL METHOD lr_json_ser->get_data
RECEIVING
rval = lv_json.
WRITE:/ lv_json .
" json 转abap
CREATE OBJECT lr_json_des.
CALL METHOD lr_json_des->deserialize
EXPORTING
json = lv_json
IMPORTING
abap = ls_data.
方法四
CALL METHOD cl_fdt_json=>data_to_json
EXPORTING
ia_data = ls_code
RECEIVING
rv_json = lv_json.
CALL METHOD cl_fdt_json=>json_to_data
EXPORTING
iv_json = lv_json
changing
ca_data = ls_code .