DATA: lv_json TYPE string.
DATA: lt_mapping_items TYPE /ui2/cl_json=>name_mappings.
DATA: BEGIN OF ls_detail,
code TYPE string,
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-code = 'A001'.
ls_detail-name = '张三'.
ls_detail-age = '18'.
APPEND ls_detail TO lt_detail.
ls_detail-code = 'A002'.
ls_detail-name = '李四'.
ls_detail-age = '25'.
APPEND ls_detail TO lt_detail.
ls_header-origh = 'C0001'.
ls_header-orname = '测试'.
ls_header-details = lt_detail.
"ABAP对应JSON字段映射
lt_mapping_items = VALUE #( ( abap = 'origh' json = 'orgid' )
( abap = 'orname' json = 'orname' )
( abap = 'details' json = 'details' )
( abap = 'code' json = 'id' )
( abap = 'name' json = 'name' )
( abap = 'age' json = 'age' )
).
"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: / 'ABAP转JSON结果:',lv_json.