背景:
在我们日常工作中,通常需要向外系统提供接口。然后当接口需要传递的信息较多时,我们通常会采用JSON的方式进行值的传递,JSON进JSON出。SAP ABAP 提供了JSON数据的处理方法我们可以通过CL_TREX_JSON_SERIALIZER CL_TREX_JSON_DESERIALIZER
案例分析:
1:数据转JSON
代码语言:javascript复制DATA:
serializer TYPE REF TO cl_trex_json_serializer,
lv_json TYPE string.
DATA: BEGIN OF ls_kv,
key1 TYPE string,
value TYPE string,
END OF ls_kv.
DATA: lt_kv LIKE HASHED TABLE OF ls_kv WITH UNIQUE KEY key1.
ls_kv-key1 = '键'.
ls_kv-value = '值'.
INSERT ls_kv INTO TABLE lt_kv.
CREATE OBJECT serializer
EXPORTING
data = ls_kv.
serializer->serialize( ).
lv_json = serializer->get_data( ).
WRITE / lv_json.
*&---------运行这段代码,输出结果,可以得到:{key:"键",value:"值"}
2:JSON 解析
代码语言:javascript复制DATA: deserializer TYPE REF TO cl_trex_json_deserializer.
CLEAR ls_kv.
CREATE OBJECT deserializer.
deserializer->deserialize(
EXPORTING json = lv_json
IMPORTING abap = ls_kv ).
WRITE: / ls_kv-key1, ls_kv-value.
*&---------运行这段代码,输出结果,可以得到: 键 值