SAP ABAP JSON格式转ABAP内表

2023-10-12 16:36:27 浏览数 (2)

实例1

代码语言:javascript复制
TYPES: BEGIN OF T_RECORD1,
         NUMNO    TYPE CHAR10,
         PRODNAME TYPE CHAR50,
       END OF T_RECORD1.
DATA: JSON1 TYPE STRING.
DATA : RECORD1 TYPE TABLE OF T_RECORD1 WITH HEADER LINE .
 
JSON1 = '[{"NumNo":"001","ProdName":"Fert01"},{"NumNo":"002","ProdName":"Fert02"}]'.
 
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = JSON1
                                     PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
                            CHANGING DATA = RECORD1[] ).
 
LOOP AT RECORD1.
  WRITE : / RECORD1-NUMNO,RECORD1-PRODNAME.
ENDLOOP.

实例2

代码语言:javascript复制
DATA : BEGIN OF LS_RECORD,
         DATALIST TYPE TABLE OF ZDATALIST_DETAIL,
       END OF LS_RECORD.
DATA: JSON TYPE STRING.
DATA : RECORD2 TYPE ZDATALIST WITH HEADER LINE.
 
JSON = '{"dataList":[{"NumNo":"001","ProdName":"Fert01"},{"NumNo":"002","ProdName":"Fert02"}]}'.
 
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = JSON
                                     PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
                            CHANGING DATA = LS_RECORD ).
 
IF LS_RECORD-DATALIST IS NOT INITIAL.
  RECORD2[] = LS_RECORD-DATALIST.
ENDIF.
 
LOOP AT RECORD2.
WRITE : / RECORD2-NUMNO,RECORD2-PRODNAME.
ENDLOOP.

0 人点赞