ABAP 把数据转JSON文本

该博客讲述了如何将不标准的JSON转换为标准格式,涉及 dataserializertypereftocl_trex_json_serializer 类型和操作,如序列化、调整键值对并创建对象。重点在于处理键未加引号的问题和使用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 "},{}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘欣的博客

你将成为第一个打赏博主的人!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值