昨天收到一个需求,循环内表中的字段,每个字段对应一个内表名称,需要根据不同的内表进行JSON转换,搞了半天没有搞定。不知为什么今天脑子突然开窍,一分钟搞定,ABAP代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
REPORT ZHELLO2. DATA: LV_STRING TYPE STRING . DATA: LT_MARA TYPE TABLE OF MARA, LT_MAKT TYPE TABLE OF MAKT. DATA: LS_MARA TYPE MARA, LS_MAKT TYPE MAKT. LS_MARA-MATNR = '11111'. APPEND LS_MARA TO LT_MARA . CLEAR LS_MARA . LS_MARA-MATNR = '22222'. APPEND LS_MARA TO LT_MARA . CLEAR LS_MARA . LS_MAKT-MATNR = '11111'. LS_MAKT-MAKTX = '11111描述'. APPEND LS_MAKT TO LT_MAKT . CLEAR LS_MAKT . LS_MAKT-MATNR = '22222'. LS_MAKT-MAKTX = '22222描述'. APPEND LS_MAKT TO LT_MAKT . CLEAR LS_MAKT . DATA: BEGIN OF LT_TABLE OCCURS 0 , TABNAME TYPE TABNAME, END OF LT_TABLE . LT_TABLE-TABNAME = 'LT_MARA'. APPEND LT_TABLE . LT_TABLE-TABNAME = 'LT_MAKT'. APPEND LT_TABLE . FIELD-SYMBOLS: <TT_DATA> TYPE ANY . " 我就想在这里循环把数据转成json LOOP AT LT_TABLE. UNASSIGN <TT_DATA>. ASSIGN (LT_TABLE-TABNAME) TO <TT_DATA>. IF <TT_DATA> IS ASSIGNED. CALL FUNCTION 'ZFM_SERIALIZE_JSON' EXPORTING IS_INPUT = <TT_DATA> IMPORTING EV_OUTPUT = LV_STRING. ENDIF. ENDLOOP. |