內錶 XML互轉 JSON 內錶互轉

2021-10-20 19:14:23 字數 3196 閱讀 3177

*轉進內錶

strans

try.

call transformation id

source xml lv_text_out

* options

* clear = 'all' "4.7 沒有這個options

* value_handling = 'accept_data_loss' "這個引數也沒有

result data = p_gt_rep.

catch cx_root into lo_ref.

lv_text = lo_ref->get_text( ).

concatenate 'rep轉換出錯:' lv_text into lv_text.

message lv_text type 's' display like 'e'.

endtry.

data lo_ref type ref to cx_root. "異常處理

data lv_text type string.

data type xstring.

函式

smum_xml_parse

*轉xml

call transformation id

options value_handling = 'move' "防止內錶中有n型別dump

source data = p_gt_rep

result xml p_**_xml.

注:

無論是xml還是json,result  後面接的都是key

*abap to json

perform frm_json_from using ls_data changing ls_requestdata-requeststring.

form frm_json_from  using uv_data changing cv_json.

data:

lo_writer type ref to cl_sxml_string_writer,

lv_json   type xstring.

lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

call transformation id

options value_handling = 'move'

source data = uv_data

result xml lo_writer.

lv_json = lo_writer->get_output( ).

cv_json = lv_json.

perform frm_xstring_to_string changing cv_json.

shift cv_json left by 8 places.

cv_json = substring( val = cv_json off = 0 len = strlen( cv_json ) - 1 ).

endform.

form frm_xstring_to_string  changing p_lv_string.

data:

lv_xstring type xstring,

length     type i,

l_cntbin   type sdokcntbins.

lv_xstring = p_lv_string.

*轉二進位制

call function 'scms_xstring_to_binary'

exporting

buffer        = lv_xstring

importing

output_length = length

tables

binary_tab    = l_cntbin.

if sy-subrc = 0.

call function 'scms_binary_to_string'

exporting

*       encoding      = '4102' "r3沒有這個引數

input_length  = length

importing

text_buffer   = p_lv_string

output_length = length

tables

binary_tab    = l_cntbin

exceptions

failed        = 1

others        = 2.

*    endif.

endif.

endform.                    " xstring_to_string

json to abap

最頂層只能是物件不能是陣列,即轉換 結果只能是個結構(可以縱深),不能是內錶

demo

perform frm_json_to using ls_in-mt_sbg_s4_comm_response-responsedata-resultstring changing ls_res.

form frm_json_to  using cv_json changing uv_data.

data lo_ref type ref to cx_root. "異常處理

data lv_text type string.

*拼個key

translate cv_json to upper case.

cv_json = ``.

try.

call transformation id source xml cv_json

result data = uv_data.

catch cx_root into lo_ref.

lv_text = lo_ref->get_text( ).

message e001(00) with '解析json失敗:' lv_text.

endtry.

endform.

JSON和SAP內錶轉化

二 內錶結構轉json josn格式,key 和value是帶雙引號的,使用 ui2 cl json deserialize反序列化 如下面的json格式 定義內錶 data begin of it itab occurs 0 sqdh 200 type c,matnr 36 type c,posn...

xml和json互轉的格式,結構

xml bpm transition class dealevent 測試3 01 2noticecheck y emailcheck y repeatcheck y remindercheck y readonlycheck y tasktime 1 timeunits 分鐘 outdeal 郵件...

xml和json互轉的格式,結構

xml bpm transition class dealevent 測試3 01 2noticecheck y emailcheck y repeatcheck y remindercheck y readonlycheck y tasktime 1 timeunits 分鐘 outdeal 郵件...