1. json 转换成内表
通过上传URL获取json数据并转换为内表
json to itab关键字
METHOD get_itab_for_json.
DATA : lr_client TYPE REF TO if_http_client,
lv_url TYPE string,
lv_content_type TYPE string VALUE 'application/x-www-form-urlencoded',
ev_xstring TYPE xstring,
ex_string TYPE string.
IF iv_guid IS INITIAL.
ev_msg = 'GUID不能为空!'.
RETURN.
ENDIF.
"get url
lv_url = zcl_wd_json=>get_system_url(
iv_system = 'DMS'
iv_action = 'GET_FILES' ).
IF lv_url IS INITIAL.
ev_msg = '未获取到URL!'.
RETURN.
ENDIF.
"get APIKEY
DATA(lv_apikey) = zcl_wd_json=>get_system_url(
iv_system = 'DMS'
iv_action = 'APIKEY' ).
lv_url = lv_url && iv_guid && |&{ lv_apikey }|.
TRY .
"创建http对象
cl_http_client=>create_by_url(
EXPORTING
url = lv_url
IMPORTING
client = lr_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
* set protocol version
CALL METHOD lr_client->request->set_header_field
EXPORTING
name = '~server_protocol'
value = 'HTTP/1.1'.
* 提交方式
CALL METHOD lr_client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'GET'.
* 传参方式: application/json,application/x-www-form-urlencoded
CALL METHOD lr_client->request->set_header_field
EXPORTING
name = 'Content-Type'
value = lv_content_type.
* 填充参数
* CLEAR lv_len.
* lv_len = strlen( iv_json ).
* IF lv_len GT 0.
* CALL METHOD lr_client->request->set_cdata " Removed APPEND_CDATA
* EXPORTING
* data = iv_json
* offset = 0
* length = lv_len.
* ENDIF.
* 发送
CALL METHOD lr_client->send
EXPORTING
timeout = 200
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
IF sy-subrc NE 0.
lr_client->get_last_error(
IMPORTING
message = ev_msg
).
RETURN.
ENDIF.
* 获取返回
CALL METHOD lr_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc NE 0.
lr_client->get_last_error(
IMPORTING
message = ev_msg
).
RETURN.
ENDIF.
* 返回
* CLEAR: lv_xjson .
ev_xstring = lr_client->response->get_data( ).
* XSTRING to STRING
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = ev_xstring
im_encoding = 'UTF-8'
IMPORTING
ex_string = ex_string.
* "json to itab
CALL TRANSFORMATION id
SOURCE XML ex_string
RESULT status = es_json-status
msg = es_json-msg
rows = es_json-rows.
CATCH cx_abapcg_exception .
ev_msg = '异常'.
ENDTRY.
* 关闭链接ie
lr_client->close( ).
ENDMETHOD.
ZTABURL01 外围系统URL配置表
METHOD get_system_url.
SELECT SINGLE url INTO ev_url FROM ztaburl01
WHERE syst = iv_system
AND action = iv_action.
ENDMETHOD.
2.JSON序列化
class-methods SERIALIZE
importing
!DATA type DATA
!COMPRESS type BOOL default C_BOOL-FALSE
!NAME type STRING optional
!PRETTY_NAME type PRETTY_NAME_MODE default PRETTY_MODE-NONE
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR optional
!ASSOC_ARRAYS type BOOL default C_BOOL-FALSE
!TS_AS_ISO8601 type BOOL default C_BOOL-FALSE
!EXPAND_INCLUDES type BOOL default C_BOOL-TRUE
!ASSOC_ARRAYS_OPT type BOOL default C_BOOL-FALSE
!NUMC_AS_STRING type BOOL default C_BOOL-FALSE
!NAME_MAPPINGS type NAME_MAPPINGS optional
returning
value(R_JSON) type JSON .
METHOD serialize.
* " **********************************************************************
* "! Usage examples and documentation can be found on SCN:
* " http://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
* " ********************************************************************** "
DATA: lo_json TYPE REF TO zcl_json.
CREATE OBJECT lo_json
EXPORTING
compress = compress
pretty_name = pretty_name
name_mappings = name_mappings
assoc_arrays = assoc_arrays
assoc_arrays_opt = assoc_arrays_opt
expand_includes = expand_includes
numc_as_string = numc_as_string
ts_as_iso8601 = ts_as_iso8601.
r_json = lo_json->serialize_int( name = name data = data type_descr = type_descr ).
ENDMETHOD.
3. 应用
3.1 SAP 获取销售系统的交房日期
FUNCTION ZFIFM_PI1225.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IT_PI1225_01) TYPE ZTPI1225_01
*" TABLES
*" ET_ZTPI1225 TYPE ZTPI1225
*"----------------------------------------------------------------------
DATA LS_SEND TYPE ZVKDT_ECC_BASE_FORM.
DATA LS_RECEIVE TYPE ZVKDT_PI1225_FWJDELVDATE_QUER4.
DATA LS_BIZPARAM TYPE ZPI1225_SBIZPARM.
DATA LT_ZTTANDCRITERIA TYPE ZTPI1225_ANDCRITERIA.
DATA LT_ZTRETURNPUBPROPS TYPE ZTPI1225_RETURNPUBPROPS.
DATA LT_MAPPING TYPE ZCL_JSON=>NAME_MAPPINGS.
DATA LR_EXCEPTION TYPE REF TO CX_AI_SYSTEM_FAULT.
DATA LCL_FAULT TYPE REF TO CX_AI_SYSTEM_FAULT.
DATA LV_ERROR_TXT TYPE STRING.
DATA LS_ZTPI1225 TYPE LINE OF ZTPI1225.
"根据房源内码取数据
LT_ZTTANDCRITERIA = VALUE #( ( FIELDCODE = 'innerId' VALUETYPE = 'String' VALUES = IT_PI1225_01 ) ).
"返回四个日期字段
APPEND 'actualPayDate' TO LT_ZTRETURNPUBPROPS.
APPEND 'noticeDate' TO LT_ZTRETURNPUBPROPS.
APPEND 'focusPayDateStart' TO LT_ZTRETURNPUBPROPS.
APPEND 'focusPayDateEnd' TO LT_ZTRETURNPUBPROPS.
LS_BIZPARAM-QUERYCRITERIA-CRITERIA-ANDCRITERIA = LT_ZTTANDCRITERIA.
LS_BIZPARAM-PAGENO = 1.
LS_BIZPARAM-RETURNAPPPROPS = LT_ZTRETURNPUBPROPS.
LS_BIZPARAM-PAGESIZE = 500.
LS_BIZPARAM-CATEGORYID = '10003'.
"和中台接口字段对应关系
LT_MAPPING = VALUE #( ( ABAP = `EXTATTRRETURNTYPE` JSON = `extAttrReturnType` )
( ABAP = `QUERYCRITERIA` JSON = `queryCriteria` )
( ABAP = `CRITERIA` JSON = `criteria` )
( ABAP = `ANDCRITERIA` JSON = `andCriteria` )
( ABAP = `FIELDCODE` JSON = `fieldCode` )
( ABAP = `VALUETYPE` JSON = `valueType` )
( ABAP = `VALUES` JSON = `values` )
( ABAP = `RETURNAPPPROPS` JSON = `returnAppProps` )
( ABAP = `PAGENO` JSON = `pageNo` )
( ABAP = `PAGESIZE` JSON = `pageSize` )
( ABAP = `CATEGORYID` JSON = `categoryId` ) ).
"BIZ_PARAM 参数业务json
LS_SEND-I_REQUEST-MESSAGE-DATA-R-BIZ_PARAM = ZCL_JSON=>SERIALIZE( DATA = LS_BIZPARAM NAME_MAPPINGS = LT_MAPPING ).
* MD5加处理
CALL FUNCTION 'ZFM_FI_ZT_SECRET_MD5'
CHANGING
IS_SEND = LS_SEND.
TRY.
CALL FUNCTION 'ZABI_0001'
EXPORTING
IV_ITFID = 'PI1225_FWJDELVDATE_QUERY'
IV_IMPORT = LS_SEND
IMPORTING
EV_EXPORT = LS_RECEIVE
EXCEPTIONS
CALL_PROXY_FAILED = 1
OTHERS = 2.
CATCH CX_AI_SYSTEM_FAULT INTO LCL_FAULT.
LV_ERROR_TXT = LCL_FAULT->GET_TEXT( ).
SY-SUBRC = 4.
ENDTRY.
DATA LO_XML TYPE REF TO CL_XML_DOCUMENT.
DATA LV_RC TYPE SY-SUBRC.
" 处理返回数据
READ TABLE LS_RECEIVE-E_RESPONSE-RETURNSTATUS-DATA INTO DATA(LS_DATA) INDEX 1.
IF SY-SUBRC = 0.
LOOP AT LS_DATA-LIST INTO DATA(LS_LIST) WHERE INNER_ID IS NOT INITIAL.
IF LS_LIST-PROP_MAP IS NOT INITIAL.
REPLACE 'fwj_focusPayDateStart' WITH 'FWJ_FOCUSPAYDATESTART' INTO LS_LIST-PROP_MAP.
REPLACE '/fwj_focusPayDateStart' WITH '/FWJ_FOCUSPAYDATESTART' INTO LS_LIST-PROP_MAP.
REPLACE 'fwj_focusPayDateEnd' WITH 'FWJ_FOCUSPAYDATEEND' INTO LS_LIST-PROP_MAP.
REPLACE '/fwj_focusPayDateEnd' WITH '/FWJ_FOCUSPAYDATEEND' INTO LS_LIST-PROP_MAP.
REPLACE 'fwj_actualPayDate' WITH 'FWJ_ACTUALPAYDATE' INTO LS_LIST-PROP_MAP.
REPLACE '/fwj_actualPayDate' WITH '/FWJ_ACTUALPAYDATE' INTO LS_LIST-PROP_MAP.
REPLACE 'fwj_noticeDate' WITH 'FWJ_NOTICEDATE' INTO LS_LIST-PROP_MAP.
REPLACE '/fwj_noticeDate' WITH '/FWJ_NOTICEDATE' INTO LS_LIST-PROP_MAP.
CREATE OBJECT LO_XML.
CALL METHOD LO_XML->PARSE_STRING
EXPORTING
STREAM = LS_LIST-PROP_MAP
RECEIVING
RETCODE = LV_RC.
CALL METHOD LO_XML->GET_DATA
CHANGING
DATAOBJECT = LS_ZTPI1225.
ENDIF.
FIND 'FWJ_FOCUSPAYDATESTART' IN LS_LIST-PROP_MAP.
IF SY-SUBRC <> 0.
* LS_ZTPI1225-FWJ_FOCUSPAYDATESTART = '99991231'.
LS_ZTPI1225-FWJ_FOCUSPAYDATESTART = ''.
ELSE.
LS_ZTPI1225-FWJ_FOCUSPAYDATESTART = LS_ZTPI1225-FWJ_FOCUSPAYDATESTART+0(4) && LS_ZTPI1225-FWJ_FOCUSPAYDATESTART+5(2) && LS_ZTPI1225-FWJ_FOCUSPAYDATESTART+8(2).
ENDIF.
FIND 'FWJ_FOCUSPAYDATEEND' IN LS_LIST-PROP_MAP.
IF SY-SUBRC <> 0.
* LS_ZTPI1225-FWJ_FOCUSPAYDATEEND = '99991231'.
LS_ZTPI1225-FWJ_FOCUSPAYDATEEND = ''.
ELSE.
LS_ZTPI1225-FWJ_FOCUSPAYDATEEND = LS_ZTPI1225-FWJ_FOCUSPAYDATEEND+0(4) && LS_ZTPI1225-FWJ_FOCUSPAYDATEEND+5(2) && LS_ZTPI1225-FWJ_FOCUSPAYDATEEND+8(2).
ENDIF.
FIND 'FWJ_ACTUALPAYDATE' IN LS_LIST-PROP_MAP.
IF SY-SUBRC <> 0.
* LS_ZTPI1225-FWJ_ACTUALPAYDATE = '99991231'.
LS_ZTPI1225-FWJ_ACTUALPAYDATE = ''.
ELSE.
LS_ZTPI1225-FWJ_ACTUALPAYDATE = LS_ZTPI1225-FWJ_ACTUALPAYDATE+0(4) && LS_ZTPI1225-FWJ_ACTUALPAYDATE+5(2) && LS_ZTPI1225-FWJ_ACTUALPAYDATE+8(2).
ENDIF.
FIND 'FWJ_NOTICEDATE' IN LS_LIST-PROP_MAP.
IF SY-SUBRC <> 0.
* LS_ZTPI1225-FWJ_NOTICEDATE = '99991231'.
LS_ZTPI1225-FWJ_NOTICEDATE = ''.
ELSE.
LS_ZTPI1225-FWJ_NOTICEDATE = LS_ZTPI1225-FWJ_NOTICEDATE+0(4) && LS_ZTPI1225-FWJ_NOTICEDATE+5(2) && LS_ZTPI1225-FWJ_NOTICEDATE+8(2).
ENDIF.
LS_ZTPI1225-INNER_ID = LS_LIST-INNER_ID.
APPEND LS_ZTPI1225 TO ET_ZTPI1225.
ENDLOOP.
ENDIF.
* 如果房源没有到日期,补个空值
SORT ET_ZTPI1225 BY INNER_ID.
CLEAR LS_ZTPI1225.
LOOP AT IT_PI1225_01 INTO DATA(LV_INNER_ID).
READ TABLE ET_ZTPI1225 TRANSPORTING NO FIELDS WITH KEY INNER_ID = LV_INNER_ID BINARY SEARCH.
IF SY-SUBRC <> 0.
LS_ZTPI1225-INNER_ID = LV_INNER_ID.
APPEND LS_ZTPI1225 TO ET_ZTPI1225.
ENDIF.
ENDLOOP.
SORT ET_ZTPI1225 BY INNER_ID.
ENDFUNCTION.
接口中台对接MD5加密
FUNCTION ZFM_FI_ZT_SECRET_MD5.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IV_KEY) TYPE STRING DEFAULT
*" EXPORTING
*" REFERENCE(EV_CIPHERTEXT) TYPE STRING
*" CHANGING
*" REFERENCE(IS_SEND) TYPE ZVKDT_ECC_BASE_FORM
*"----------------------------------------------------------------------
DATA LV_PLAINTEXT TYPE STRING.
DATA LV_DATUM TYPE CHAR10.
DATA LV_UZEIT TYPE CHAR10.
DATA LS_ZTAB0041 TYPE ZTAB0041.
LV_DATUM = SY-DATUM+0(4) && '-' && SY-DATUM+4(2) && '-' && SY-DATUM+6(2).
WRITE SY-UZEIT TO LV_UZEIT.
" 通用参数设置
SELECT SINGLE * FROM ZTAB0041 INTO LS_ZTAB0041.
IS_SEND-I_REQUEST-MESSAGE-DATA-R-V = LS_ZTAB0041-VERSION.
IS_SEND-I_REQUEST-MESSAGE-DATA-R-APP_ID = LS_ZTAB0041-APPID.
IS_SEND-I_REQUEST-MESSAGE-DATA-R-DOMAIN = LS_ZTAB0041-ZDOMAIN.
IS_SEND-I_REQUEST-MESSAGE-DATA-R-USER_DOMAIN = LS_ZTAB0041-USER_DOMAIN.
IS_SEND-I_REQUEST-MESSAGE-DATA-R-SIGN_METHOD = 'md5'.
IS_SEND-I_REQUEST-MESSAGE-DATA-R-REQ_ID = LS_ZTAB0041-REQ_ID.
CONCATENATE LV_DATUM LV_UZEIT INTO IS_SEND-I_REQUEST-MESSAGE-DATA-R-TIMESTAMP SEPARATED BY SPACE.
" 输入参数处理
LV_PLAINTEXT = LS_ZTAB0041-APPSECRET
&& 'appId' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-APP_ID
&& 'bizParam' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-BIZ_PARAM
&& 'domain' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-DOMAIN
&& 'reqId' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-REQ_ID
&& 'signMethod' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-SIGN_METHOD
&& 'timestamp' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-TIMESTAMP
&& 'userDomain' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-USER_DOMAIN
&& 'v' && IS_SEND-I_REQUEST-MESSAGE-DATA-R-V
&& LS_ZTAB0041-APPSECRET.
" MD5 加密处理
CALL FUNCTION 'CALCULATE_HASH_FOR_CHAR'
EXPORTING
ALG = 'MD5'
DATA = LV_PLAINTEXT
IMPORTING
HASHSTRING = IS_SEND-I_REQUEST-MESSAGE-DATA-R-SIGN.
ENDFUNCTION.