ABAP 和JSON 转换方法

REPORT ztest_json.
DATA: json_ser TYPE REF TO cl_trex_json_serializer,
json_des TYPE REF TO zcl_trex_json_deserializer.

TYPES:BEGIN OF zsml,
functionname(30),
bcpcode(30),
status(1),
msg(256),
result(1024),
END OF zsml.

DATA ls_json TYPE zsml.
DATA lt_json TYPE TABLE OF zsml.
DATA lv_json TYPE string.

* 设置报文内容(json格式字符串)
ls_json-functionname = 'XXXXXX'.
ls_json-bcpcode = '20170214000000'.
ls_json-status = 'S'.
ls_json-msg = '请调用接口抓取数据'.
ls_json-result = ''.
APPEND ls_json TO lt_json.

* 内表->JSON
CREATE OBJECT json_ser
EXPORTING
DATA = lt_json[].
CALL METHOD json_ser->serialize.
CALL METHOD json_ser->get_data
RECEIVING
rval = lv_json.


*JSON->内表
CREATE OBJECT json_des.
CLEAR lt_json.
CALL METHOD json_des->deserialize
EXPORTING
json = lv_json
IMPORTING
abap = lt_json[].

posted @ 2017-12-01 10:15  园友1017315  阅读(1782)  评论(0编辑  收藏  举报