ABAP 把数据转JSON文本
data serializer type ref to cl_trex_json_serializer.
types: begin of type_s1,
key type string,
value type string,
end of type_s1.
data lt_json type table of type_s1.
data ls_json type type_s1.
ls_json-key = 'country'.
ls_json-value = 'cn'.
APPEND ls_json to lt_json.
ls_json-key = 'QA'.
ls_json-value = '1000'.
APPEND ls_json to lt_json.
ls_json-key = 'BS'.
ls_json-value = '2000'.
APPEND ls_json to lt_json.
ls_json-key = 'PS'.
ls_json-value = '3000'.
APPEND ls_json to lt_json.
ls_json-key = 'GA'.
ls_json-value = '4000'.
APPEND ls_json to lt_json.
create object serializer
exporting
data = lt_json.
serializer->serialize( ).
write: / serializer->get_data( ).
---------2021.9.14---------------
上面的JSON并不标准,字段名称没有使用双引号
需要换成下面这个:
"3、产生JSON
data serializer type ref to cl_trex_json_serializer.
types: begin of type_s1,
item type string,
end of type_s1.
data lt_json type table of type_s1.
data ls_json type type_s1.
ls_json-item = lv_QUANT.
APPEND ls_json to lt_json.
ls_json-item = ls_ord_rfc_jiaoyan-COUNTRY.
APPEND ls_json to lt_json.
ls_ord_rfc_jiaoyan-COUNTRY = /ui2/cl_json=>serialize( data = lt_json compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
产生的JSON文本:
[{"item":"31.000 "},{}]
post SAP-Garson
原文链接:https://blog.csdn.net/ot512csdn/article/details/120173041文章来自于网络,如果侵犯了您的权益,请联系站长删除!