FUNCTION ZRFC_FI_SEND_PAYPLAN2BPM.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(INPUT) TYPE ZSRFC_FI_SEND_PAYBPM_IN
*" EXPORTING
*" VALUE(OUTPUT) TYPE ZRFC_RETURN
*"----------------------------------------------------------------------DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT.DATA: LO_WRITER TYPE REF TO CL_SXML_STRING_WRITER.DATA:LV_ERR_TEXT TYPE STRING,LV_JSON_OUT TYPE XSTRING,LV_REQUEST TYPE STRING,LV_URL TYPE AGR_URL,LV_OUTPUT_LENGTH TYPE I,LV_URL_STR TYPE STRING,LT_BINARY_TAB TYPE STANDARD TABLE OF SDOKCNTBIN,RESULT TYPE STRING,LV_SERVER_NAME TYPE ZE_SERVER_NAME,LS_ITEM TYPE ZPDT_SEND_PAYBPM_REQ_DATA,LT_ITEM TYPE TABLE OF ZPDT_SEND_PAYBPM_REQ_DATA,LV_LEN TYPE I.LV_SERVER_NAME = 'PAYPLAN'.SELECT SINGLE HOSTINTO LV_URLFROM /THINKWAY/TBUS0AWHERE EXCHANGE_SYSTEM = 'BPM'AND INSTANCE = 'PAYPLAN'.IF SY-SUBRC <> 0.OUTPUT-MSGTY = 'E'.OUTPUT-MSGTX = '未找到目标地址'.ELSE.LV_URL_STR = LV_URL.CONDENSE LV_URL_STR.CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URLEXPORTINGURL = LV_URL_STRIMPORTINGCLIENT = LO_HTTP_CLIENTEXCEPTIONSARGUMENT_NOT_FOUND = 1PLUGIN_NOT_ACTIVE = 2INTERNAL_ERROR = 3OTHERS = 4.*content_typeLO_HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' ).LO_HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).CLEAR LT_ITEM[].LOOP AT INPUT-ITEM INTO DATA(LS_INPUT).LS_INPUT-LIFNR = |{ LS_INPUT-LIFNR ALPHA = OUT }|.LS_INPUT-BELNR = |{ LS_INPUT-BELNR ALPHA = OUT }|.LS_INPUT-HKONT = |{ LS_INPUT-HKONT ALPHA = OUT }|.MOVE-CORRESPONDING LS_INPUT TO LS_ITEM.CONDENSE LS_ITEM-ZZDMBTR NO-GAPS.CONDENSE LS_ITEM-WRBTR NO-GAPS.LS_ITEM-ZFBDT = LS_ITEM-BUDAT.APPEND LS_ITEM TO LT_ITEM.ENDLOOP.LO_WRITER = CL_SXML_STRING_WRITER=>CREATE( TYPE = IF_SXML=>CO_XT_JSON ).CALL TRANSFORMATION ID SOURCE DATA = LT_ITEM[] RESULT XML LO_WRITER.LV_JSON_OUT = LO_WRITER->GET_OUTPUT( ).CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'EXPORTINGBUFFER = LV_JSON_OUTIMPORTINGOUTPUT_LENGTH = LV_OUTPUT_LENGTHTABLESBINARY_TAB = LT_BINARY_TAB.CALL FUNCTION 'SCMS_BINARY_TO_STRING'EXPORTINGINPUT_LENGTH = LV_OUTPUT_LENGTHIMPORTINGTEXT_BUFFER = LV_REQUESTOUTPUT_LENGTH = LV_OUTPUT_LENGTHTABLESBINARY_TAB = LT_BINARY_TAB.LV_LEN = STRLEN( LV_REQUEST ).REPLACE ALL OCCURRENCES OF REGEX '"CONTROLLER":\[\],' IN LV_REQUEST WITH ''.
*- 通过PO抓取BPM的数据LV_LEN = STRLEN( LV_REQUEST ).CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELDEXPORTINGNAME = '~request_method'VALUE = 'POST'.CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATAEXPORTINGDATA = LV_REQUESTOFFSET = 0LENGTH = LV_LEN.CALL METHOD LO_HTTP_CLIENT->SENDEXCEPTIONSHTTP_COMMUNICATION_FAILURE = 1HTTP_INVALID_STATE = 2HTTP_PROCESSING_FAILED = 3HTTP_INVALID_TIMEOUT = 4OTHERS = 5."接收CALL METHOD LO_HTTP_CLIENT->RECEIVEEXCEPTIONSHTTP_COMMUNICATION_FAILURE = 1HTTP_INVALID_STATE = 2HTTP_PROCESSING_FAILED = 3.RESULT = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).LO_HTTP_CLIENT->CLOSE( ).DATA: LJSONNODE TYPE REF TO ZCL_MDP_JSON_NODE.DATA:L_VALUEOBJ TYPE REF TO ZCL_MDP_JSON_NODE.LJSONNODE = ZCL_MDP_JSON_NODE=>DESERIALIZE( JSON = RESULT ).FREE LO_HTTP_CLIENT.L_VALUEOBJ = LJSONNODE->OBJECT_GET_CHILD_NODE( KEY = 'isSuccess' ).IF L_VALUEOBJ->VALUE = 'true'.OUTPUT-MSGTY = 'S'.OUTPUT-MSGTX = LJSONNODE->OBJECT_GET_CHILD_NODE( KEY = 'errorMsg' )->VALUE.ELSE.OUTPUT-MSGTY = 'E'.OUTPUT-MSGTX = LJSONNODE->OBJECT_GET_CHILD_NODE( KEY = 'errorMsg' )->VALUE.ENDIF.ENDIF.
ENDFUNCTION.
以上 ABAP转json后会产生
"DATA":{"LT_ITEM":[{"CONTROLLER":[],"BUKRS":"1040","BUTXT":"XXXXX机械科技有限公司","LIFNR":"2000","NAME1":"XXXXX科技有限公司","BELNR":"100027622","BUZEI":"001","BUDAT":"20240930","BLDAT":"20240930","WRBTR":"49041.11","ZTERM":"C999","ZFBDT":"20240930","ZFAEL":"31","ZZ....
的数据,而实际的 {"CONTROLLER":[],是不需要的
所以要用到 REPLACE ALL OCCURRENCES OF REGEX '"CONTROLLER":\[\],' IN LV_REQUEST WITH ''.
本来replace就可以了,因为有多个 所以 用到 REPLACE ALL OCCURRENCES OF REGEX
注意 '"CONTROLLER":\[\],' 因为特殊符号 [] 在,所以要用到转义一下
参考数据
写于20241010 台州