Json数据类型对应ABAP的定义
json | abap | 备注 |
字符串 | string、char | |
数字 | p、i | |
对象 | 结构体、内表 | |
数组 | string | 需要而外写逻辑处理 |
布尔 | abap_bool | abap的布尔x表示true,空表示false |
假设外部系统需要的数据是这样的
而ABAP内表数据是这样的
直接上代码
TYPES: BEGIN OF ty_product, name TYPE string, "商品名称 number TYPE i, "数据 price TYPE p DECIMALS 2, "价格 storage_site TYPE string, "储存位置 promotion_flag TYPE abap_bool, "是否为促销商品 END OF ty_product, tyt_product TYPE STANDARD TABLE OF ty_product WITH DEFAULT KEY, BEGIN OF ty_data, products TYPE tyt_product, END OF ty_data. DATA: ls_data TYPE ty_data, ls_product TYPE ty_product, lv_json TYPE string. DATA: lt_storage_site TYPE TABLE OF string, ls_storage_site LIKE LINE OF lt_storage_site, lv_table_lines TYPE i, lv_storage_site TYPE string. ls_product-name = '苹果'. ls_product-number = 100. ls_product-price = 1088 / 100. ls_product-promotion_flag = 'X'. ls_product-storage_site = '水果区,2号货架'. APPEND ls_product TO ls_data-products. CLEAR ls_product. ls_product-name = '香梨'. ls_product-number = 200. ls_product-price = 888 / 100. ls_product-storage_site = '水果区,2号货架'. APPEND ls_product TO ls_data-products. CLEAR ls_product. "abap数据转json /ui2/cl_json=>serialize( EXPORTING data = ls_data pretty_name = 'X' "空大写,L小写,X驼峰,Y增强驼峰 RECEIVING r_json = lv_json ). "储存位置是数组数据,需要额外处理 LOOP AT ls_data-products INTO ls_product. CLEAR: lt_storage_site,lv_storage_site. "将储存位置用逗号分割到内表里 SPLIT ls_product-storage_site AT ',' INTO TABLE lt_storage_site. "计算内表行数 lv_table_lines = lines( lt_storage_site ). "拼接成数组 lv_storage_site = '['. LOOP AT lt_storage_site INTO ls_storage_site. "最后一行数据不用拼接,号 IF sy-tabix = lv_table_lines. lv_storage_site = lv_storage_site && '"'&& ls_storage_site && '"'. ELSE. lv_storage_site = lv_storage_site && '"'&& ls_storage_site && '",'. ENDIF. ENDLOOP. lv_storage_site = lv_storage_site && ']'. "要替换的内容增加引号 ls_product-storage_site = '"' && ls_product-storage_site && '"'. "替换 REPLACE ls_product-storage_site WITH lv_storage_site INTO lv_json. ENDLOOP. cl_demo_output=>write( ls_data-products ). cl_demo_output=>write( '转换json后' ). cl_demo_output=>write( lv_json ). cl_demo_output=>display( ).
标签:product,TYPE,storage,site,lv,ABAP,ls,Json,数据 From: https://www.cnblogs.com/StephenAmell/p/16775029.html