ABAP内表数据转换为JSON格式

SAP需要将内表数据转换为JSON格式,可以使用 CL_FDT_JSON。

 1 TYPES: BEGIN OF ty_p,
 2         name TYPE c LENGTH 10,
 3         age TYPE i,
 4       END OF ty_p.
 5 
 6 DATA lt_p1 TYPE TABLE OF ty_p.
 7 DATA lt_p2 TYPE TABLE OF ty_p.
 8 DATA ls_p TYPE ty_p.
 9 DATA ls_json TYPE string.
10 
11 ls_p-name = 'Peter'.
12 ls_p-age = 18.
13 APPEND ls_p TO lt_p1.
14 
15 ls_p-name = 'John'.
16 ls_p-age = 28.
17 APPEND ls_p TO lt_p1.
18 
19 CALL METHOD cl_fdt_json=>data_to_json 20 EXPORTING 21 ia_data = lt_p1 22 RECEIVING 23 rv_json = ls_json. 24 25 WRITE ls_json.

结果:

 

 如果需要将JSON数据转换为内表格式,也很简单。

1 CALL METHOD cl_fdt_json=>json_to_data
2   EXPORTING
3     iv_json = ls_json
4   CHANGING
5     ca_data = lt_p2.

 

posted @ 2020-08-31 13:43  北斗星君  阅读(1741)  评论(0编辑  收藏  举报