ABAP解析返回的JSON 案例
DATA:LTOKEN TYPE STRING.
DATA: LL_LINES(10),"行数
LL_TABIX(10),"循环标号
LL_PECNT TYPE P LENGTH 6 DECIMALS 2, "百分比
LL_PECET(6),"百分数
LL_TEXT(40)."消息
CLEAR: LL_LINES,LL_TABIX,LL_PECNT,LL_PECET,LL_TEXT.
* CALL FUNCTION 'ZWMS_XHY_GET_TOKEN'
* IMPORTING
* LV_RESULT = LTOKEN.
DATA: LEN TYPE I, "发送报文长度
LEN_STRING TYPE STRING,
URL TYPE STRING, "接口地址
LV_URL TYPE AGR_URL,
LV_URL_STR TYPE STRING,
HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, "http客户端
LV_SERVER_NAME TYPE ZE_SERVER_NAME,
POST_STRING TYPE STRING,
RESULT TYPE STRING,
LTIMEEND TYPE STRING, "时间戳结束
LTIMEST TYPE STRING. "时间戳开始
LV_URL_STR = 'your URL '.
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL_STR
IMPORTING
CLIENT = HTTP_CLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
"设定传输请求内容格式以及编码格式
HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' ).
"设置http method 为Get
HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).
DATA:LVJSON TYPE STRING.
DATA:L_UUID32 TYPE SYSUUID_C32,
ILOGS LIKE ZRFCLOGS OCCURS 0 WITH HEADER LINE.
DATA:ITEM TYPE I. "行项目
ITEM = 0.
LVJSON = '{"SQL":"SELECT top 20 MATNR from ZVMARC "}'. //your json 参数
LEN = STRLEN( LVJSON ).
ILOGS-CALPARMI = LVJSON.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~request_method'
VALUE = 'POST'.
"设置传入字符串
CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LVJSON
OFFSET = 0
LENGTH = LEN.
"发送
CALL METHOD HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
"提取返回字符串
RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
DATA LS_CHAR TYPE C.
LS_CHAR = CL_ABAP_CHAR_UTILITIES=>CR_LF. " 获取换行符
DATA: L_JSON_ROOT_OBJECT TYPE REF TO ZCL_MDP_JSON_NODE.
DATA: L_ERRORTYPE_OBJECT TYPE REF TO ZCL_MDP_JSON_NODE.
DATA: L_SAP_TEST_OBJECT TYPE REF TO ZCL_MDP_JSON_NODE.
DATA: L_ERRORNO_OBJECT TYPE REF TO ZCL_MDP_JSON_NODE.
DATA: L_SAP_TEST_ROW1_OBJECT TYPE REF TO ZCL_MDP_JSON_NODE.
DATA: L_SAP_TEST_FUNC_ID_OBJECT TYPE REF TO ZCL_MDP_JSON_NODE.
DATA: LTAR TYPE TABLE OF ZCL_MDP_JSON_NODE=>TYP_ARRAY_CHILDREN.
L_JSON_ROOT_OBJECT = ZCL_MDP_JSON_NODE=>DESERIALIZE( JSON = RESULT ).
DATA: L_STRING TYPE STRING.
L_ERRORTYPE_OBJECT = L_JSON_ROOT_OBJECT->OBJECT_GET_CHILD_NODE( KEY = 'RESULT').
LTAR = L_ERRORTYPE_OBJECT->ARRAY_CHILDREN().
LOOP AT LTAR INTO DATA(LSAR).
L_SAP_TEST_OBJECT = LSAR-NODE.
L_SAP_TEST_FUNC_ID_OBJECT = L_SAP_TEST_OBJECT->OBJECT_GET_CHILD_NODE( KEY = 'MATNR' ).
WRITE :/ L_SAP_TEST_FUNC_ID_OBJECT->VALUE.
ENDLOOP.
WRITE:/ '123'.