本篇內(nèi)容介紹了“abap結(jié)構(gòu)或內(nèi)表怎么實(shí)現(xiàn)與json字符串相互轉(zhuǎn)換.”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
可使用的類有很多,如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)在于字段名是否有"號包含,1的方式使用標(biāo)準(zhǔn)json格式無法解析,2更通用。
“abap結(jié)構(gòu)或內(nèi)表怎么實(shí)現(xiàn)與json字符串相互轉(zhuǎn)換.”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
文章題目:abap結(jié)構(gòu)或內(nèi)表怎么實(shí)現(xiàn)與json字符串相互轉(zhuǎn)換.-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://chinadenli.net/article4/dosjoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站改版、外貿(mào)建站、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容