1 *例子1
2 REPORT y4_b4_test8.
3 DATA:lt_ekko TYPE zheaders,
4 gt_ekko TYPE zheaders,
5 gs_ekko TYPE zheader,
6 gt_ekpo TYPE zitems,
7 gs_ekpo TYPE zitem,
8 xml_xstring TYPE xstring,
9 xml_string TYPE string,
10 lo_oref TYPE REF TO cx_st_error,
11 lv_message TYPE char200. "输出参数
12
13 "行结构
14 gs_ekpo-buzei = '001'.
15 gs_ekpo-bschl = '22'.
16 gs_ekpo-kostl = '55555' .
17 gs_ekpo-hkont = '99999' .
18 gs_ekpo-matnr = 'MM01' .
19 gs_ekpo-werks = '1710' .
20 APPEND gs_ekpo TO gt_ekpo.
21 APPEND gs_ekpo TO gt_ekpo.
22
23 "头结构
24 gs_ekko-bukrs = '1310'.
25 gs_ekko-gjahr = '2022'.
26 gs_ekko-belnr = '2022042610'.
27 gs_ekko-item = gt_ekpo.
28 APPEND gs_ekko TO gt_ekko.
29 APPEND gs_ekko TO gt_ekko.
30
31 DATA: lv_em TYPE string,
32 lv_em1 TYPE string.
33
34 "数据
35 lv_em = 'test' .
36
37 " abap 转json
38 DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
39 CALL TRANSFORMATION id
40 SOURCE content = lv_em
41 itable = gt_ekko
42 RESULT XML json_writer.
43 DATA(json) = json_writer->get_output( ).
44 DATA: lv_json TYPE string .
45 CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
46 EXPORTING
47 im_xstring = json
48 im_encoding = 'UTF-8'
49 IMPORTING
50 ex_string = lv_json.
51
52 CALL TRANSFORMATION id
53 SOURCE XML lv_json
54 RESULT content = lv_em1
55 itable = lt_ekko.
1 *例子2
2 REPORT y4_b4_test8.
3 DATA:lt_ekko TYPE zheaders,
4 gt_ekko TYPE zheaders,
5 gs_ekko TYPE zheader,
6 gt_ekpo TYPE zitems,
7 gs_ekpo TYPE zitem,
8 xml_xstring TYPE xstring,
9 xml_string TYPE string,
10 lo_oref TYPE REF TO cx_st_error,
11 lv_message TYPE char200. "输出参数
12
13 DATA ls_json TYPE string.
14
15 "行结构
16 gs_ekpo-buzei = '001'.
17 gs_ekpo-bschl = '22'.
18 gs_ekpo-kostl = '55555' .
19 gs_ekpo-hkont = '99999' .
20 gs_ekpo-matnr = 'MM01' .
21 gs_ekpo-werks = '1710' .
22 APPEND gs_ekpo TO gt_ekpo.
23 APPEND gs_ekpo TO gt_ekpo.
24
25 "头结构
26 gs_ekko-bukrs = '1310'.
27 gs_ekko-gjahr = '2022'.
28 gs_ekko-belnr = '2022042610'.
29 gs_ekko-item = gt_ekpo.
30 APPEND gs_ekko TO gt_ekko.
31 APPEND gs_ekko TO gt_ekko.
32
33 CALL METHOD cl_fdt_json=>data_to_json
34 EXPORTING
35 ia_data = gt_ekko
36 RECEIVING
37 rv_json = ls_json.
38 WRITE ls_json.
39
40 CALL METHOD cl_fdt_json=>json_to_data
41 EXPORTING
42 iv_json = ls_json
43 CHANGING
44 ca_data = lt_ekko.