本篇內(nèi)容介紹了“abap結(jié)構(gòu)或內(nèi)表怎么實(shí)現(xiàn)與json字符串相互轉(zhuǎn)換.”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括互助網(wǎng)站建設(shè)、互助網(wǎng)站制作、互助網(wǎng)頁制作以及互助網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,互助網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到互助省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
可使用的類有很多,如cl_trex_json_serializer與cl_trex_json_deserializer,SAP也為我們提供了CL_FDT_JSON, /UI2/CL_JSON等來進(jìn)行JSON的序列化與反序列化。
由于sap的版本問題,可能某些類在服務(wù)器上不存在而無法使用。該 博客 作者詳細(xì)描述了如何用abap代碼使用序列化與反序列化,
同時(shí)提供了/UI2/CL_JSON的代碼,可供 下載 使用。
在此我提供一個(gè)將/UI2/CL_JSON代碼處理好的includeZBC_UI2_JSON_CLS.txt,供大家在缺少該類的情況下,添加到主程序中使用。
------------------------------------------------------------------------------------------------------------------------------------------
1、如何使用cl_trex_json_serializer與cl_trex_json_deserializer實(shí)現(xiàn)序列化與反序列化。
(我的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轉(zhuǎn)化的不同點(diǎn)在于字段名是否有"號(hào)包含,1的方式使用標(biāo)準(zhǔn)json格式無法解析,2更通用。
“abap結(jié)構(gòu)或內(nèi)表怎么實(shí)現(xiàn)與json字符串相互轉(zhuǎn)換.”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!