分享一个三层表格转换伙JASON格式的例子,代码如下。
REPORT zjason_test.
"// 定义
DATA: lv_json TYPE string.
DATA: BEGIN OF ls_detail_l3,
code_l3 TYPE string,
name_l3 TYPE string,
age_l3 TYPE string,
END OF ls_detail_l3,
lt_detail_l3 LIKE TABLE OF ls_detail_l3.
DATA: BEGIN OF ls_detail,
code TYPE string,
details_l3 LIKE lt_detail_l3,
name TYPE string,
age TYPE string,
END OF ls_detail,
lt_detail LIKE TABLE OF ls_detail,
BEGIN OF ls_header,
origh TYPE string,
orname TYPE string,
details LIKE lt_detail,
END OF ls_header.
ls_detail_l3-code_l3 = '第三层00_l3'.
ls_detail_l3-name_l3 = '三层_l3'.
ls_detail_l3-age_l3 = '18_l3'.
APPEND ls_detail_l3 TO lt_detail_l3.
ls_detail-code = '第二层001'.
ls_detail-name = '张三'.
ls_detail-age = '18'.
ls_detail-details_l3 = lt_detail_l3.
APPEND ls_detail TO lt_detail.
ls_detail-code = '第二层002'.
ls_detail-name = '李四'.
ls_detail-age = '25'.
APPEND ls_detail TO lt_detail.
ls_header-origh = '第一层0001'.
ls_header-orname = '测试'.
ls_header-details = lt_detail.
"ABAP结构转JSON
lv_json = /ui2/cl_json=>serialize(
data = ls_header
compress = abap_false
pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
" name_mappings = lt_mapping_items ).
WRITE: / '转换结果:',lv_json.
输出结果如下: