ABAP內表數據轉換爲JSON格式

SAP須要將內表數據轉換爲JSON格式,可使用 CL_FDT_JSON。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.

結果:spa

 

 若是須要將JSON數據轉換爲內表格式,也很簡單。code

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

相關文章
相關標籤/搜索