SAP 自定义解析JSON方法

2023-10-13 18:57:15 浏览数 (2)

代码语言:javascript复制
JSON串:
 HEADER = ‘{“STATUS”:“1”,“BSART”:“SH02”,“LIFNR”:“0040003799”,“EKORG”:“SHM1”,“EKGRP”:“SH2”,“BUKRS”:“5200”,“ZCMISNO”:"",“ZCRTNAME”:"",“ZSWZXR”:"",“LONGTEXT”:"",“ERNAM”:"",“WAERS”:"",“EBELNESSC”:""}’.

FORM analysis_json TABLES tab_alv TYPE STANDARD TABLE
 USING t
 CHANGING gv_text status ebelnessc tdline.
 FIELD-SYMBOLS: .
DATA:BEGIN OF tab_j,
 txt(262143) TYPE c,
 END OF tab_j.
 DATA:tab1 LIKE TABLE OF tab_j WITH HEADER LINE.
 DATA:tab2 LIKE TABLE OF tab_j WITH HEADER LINE.
 DATA:lv_fld_name TYPE string.
 DATA:key TYPE string.
 DATA:value TYPE string.
 REPLACE ALL OCCURRENCES OF ‘[’ IN t WITH ‘’.
 REPLACE ALL OCCURRENCES OF ‘]’ IN t WITH ‘’.
 SPLIT t AT ‘}’ INTO TABLE tab1.
 LOOP AT tab1.
 REPLACE ALL OCCURRENCES OF ‘{’ IN tab1-txt WITH ‘’.
 SPLIT tab1-txt AT ‘,’ INTO TABLE tab2.
 LOOP AT tab2.
 IF tab2-txt <> ‘’.
 CLEAR:key,value.
 REPLACE ALL OCCURRENCES OF ‘"’ IN tab2-txt WITH ‘’.
 SPLIT tab2-txt AT ‘:’ INTO key value.
 CONDENSE key NO-GAPS.
 CONDENSE value NO-GAPS.
 IF key = ‘PS_PSP_PNR’.
 SELECT SINGLE pspnr INTO value FROM prps WHERE posid = value.
 ENDIF.
 IF value = ‘NULL’.
 value = ‘’.
 ENDIF.
 IF value = ‘null’.
 value = ‘’.
 ENDIF.
    IF key = 'LONGTEXT'.
      gv_text = value.
    ELSEIF key = 'STATUS'.
      status = value.
    ELSEIF key = 'EBELNESSC'.
      ebelnessc = value.
    ELSEIF key = 'TDLINE'.
      tdline = value.
    ELSE.
      CONCATENATE 'TAB_ALV-' key INTO lv_fld_name.
      ASSIGN (lv_fld_name) TO <fs1>.
      <fs1> = value.
    ENDIF.
  ENDIF.
ENDLOOP.
APPEND tab_alv.
CLEAR:tab_alv.
CLEAR:tab2,tab2[].
ENDLOOP.
 ENDFORM.

0 人点赞