重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“abap结构或内表怎么实现与json字符串相互转换.”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联专注于企业全网营销推广、网站重做改版、任县网站定制设计、自适应品牌网站建设、H5页面制作、商城建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为任县等各大城市提供网站开发制作服务。可使用的类有很多,如cl_trex_json_serializer与cl_trex_json_deserializer,SAP也为我们提供了CL_FDT_JSON, /UI2/CL_JSON等来进行JSON的序列化与反序列化。
由于sap的版本问题,可能某些类在服务器上不存在而无法使用。该 博客 作者详细描述了如何用abap代码使用序列化与反序列化,
同时提供了/UI2/CL_JSON的代码,可供 下载 使用。
在此我提供一个将/UI2/CL_JSON代码处理好的includeZBC_UI2_JSON_CLS.txt,供大家在缺少该类的情况下,添加到主程序中使用。
------------------------------------------------------------------------------------------------------------------------------------------
1、如何使用cl_trex_json_serializer与cl_trex_json_deserializer实现序列化与反序列化。
(我的sap版本中没有类cl_trex_json_deserializerZBC_CL_TREX_JSON_DESERIALIZER.txt,include供大家上传使用)
REPORT yaiolos_test_06.
" **********************************************************************
"! Usage examples and documentation can be found on SCN:
" http://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
" ********************************************************************** "
INCLUDE zbc_ui2_json_cls.
INCLUDE zbc_cl_trex_json_deserializer.
DATA: lt_flight TYPE STANDARD TABLE OF sflight WITH HEADER LINE,
lv_json TYPE string,
lv_json_out TYPE string,
lv_json_len TYPE i.
DATA:
serializer TYPE REF TO cl_trex_json_serializer,
deserializer TYPE REF TO zcl_trex_json_deserializer.
START-OF-SELECTION.
***序列化
**********************************************************************
*Usage examples and documentation can be found on https://www.cnblogs.com/hhelibeb/p/6617488.html
**********************************************************************
SELECT * FROM sflight INTO TABLE lt_flight.
CREATE OBJECT serializer
EXPORTING
DATA = lt_flight[].
serializer->serialize( ).
lv_json = serializer->get_data( ).
lv_json_out = lv_json.
DO.
lv_json_len = strlen( lv_json_out ).
IF lv_json_len < 100.
WRITE / lv_json_out.
EXIT.
ELSE.
WRITE / lv_json_out+100.
ENDIF.
lv_json_out = lv_json_out+100.
ENDDO.
***反序列化
REFRESH lt_flight.
CREATE OBJECT deserializer.
deserializer->deserialize(
EXPORTING json = lv_json
IMPORTING abap = lt_flight[] ).
LOOP AT lt_flight.
WRITE /: lt_flight-carrid.
ENDLOOP.
2、使用/ui2/cl_json来序列化和反序列化json字符串。
REPORT yaiolos_test_07.
" **********************************************************************
"! Usage examples and documentation can be found on SCN:
" http://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
" ********************************************************************** "
INCLUDE zbc_ui2_json_cls.
INCLUDE zbc_cl_trex_json_deserializer.
DATA: lt_flight TYPE STANDARD TABLE OF sflight,
ls_flight TYPE sflight,
lrf_descr TYPE REF TO cl_abap_typedescr,
lv_json TYPE string.
DATA : lv_json_len TYPE i,
lv_json_out TYPE string.
DEFINE macro_write.
lv_json_out = &1.
do.
lv_json_len = strlen( lv_json_out ).
if lv_json_len < 100.
write / lv_json_out.
exit.
else.
write / lv_json_out+100.
endif.
lv_json_out = lv_json_out+100.
enddo.
END-OF-DEFINITION.
START-OF-SELECTION.
SELECT * FROM sflight INTO TABLE lt_flight.
* serialize table lt_flight into JSON, skipping initial fields and converting ABAP field names into camelCase
lv_json = zui2_json=>serialize( data = lt_flight compress = abap_true pretty_name = zui2_json=>pretty_mode-camel_case ).
macro_write lv_json.
CLEAR lt_flight.
* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping
zui2_json=>deserialize( EXPORTING json = lv_json pretty_name = zui2_json=>pretty_mode-camel_case CHANGING data = lt_flight ).
LOOP AT lt_flight INTO ls_flight.
WRITE / ls_flight-carrid.
ENDLOOP.
与1转化的不同点在于字段名是否有"号包含,1的方式使用标准json格式无法解析,2更通用。
“abap结构或内表怎么实现与json字符串相互转换.”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!