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_URLEXPORTINGURL = LV_URL_STRIMPORTINGCLIENT = HTTP_CLIENTEXCEPTIONSARGUMENT_NOT_FOUND = 1PLUGIN_NOT_ACTIVE = 2INTERNAL_ERROR = 3OTHERS = 4."设定传输请求内容格式以及编码格式HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' )."设置http method 为GetHTTP_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_FIELDEXPORTINGNAME = '~request_method'VALUE = 'POST'."设置传入字符串CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATAEXPORTINGDATA = LVJSONOFFSET = 0LENGTH = LEN."发送CALL METHOD HTTP_CLIENT->SENDEXCEPTIONSHTTP_COMMUNICATION_FAILURE = 1HTTP_INVALID_STATE = 2HTTP_PROCESSING_FAILED = 3HTTP_INVALID_TIMEOUT = 4OTHERS = 5."接收CALL METHOD HTTP_CLIENT->RECEIVEEXCEPTIONSHTTP_COMMUNICATION_FAILURE = 1HTTP_INVALID_STATE = 2HTTP_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'.