TYPES:BEGIN OF gty_data1,
index TYPE i,
pernrTYPE pa0001-pernr,
nameTYPE pa0002-nachn,
sexTYPE char2,
phoneTYPE usrid,
END OF gty_data1,
BEGIN OF gty_data2,
index TYPE i,
pernrTYPE pa0001-pernr,
phoneTYPE usrid,
END OF gty_data2,
BEGIN OF gty_data3,
index TYPE string,
pernrTYPE string,
nameTYPE string,
sexTYPE string,
phoneTYPE string,
END OF gty_data3.
DATA:gt_data1TYPE TABLE OF gty_data1,
gt_data2TYPE TABLE OF gty_data2,
gt_data3TYPE TABLE OF gty_data3,
gt_fcatTYPE lvc_t_fcat.
DATA:gs_data1TYPE gty_data1,
gs_data2TYPE gty_data2,
gs_data3TYPE gty_data3,
gs_fcatTYPE lvc_s_fcat.
DATA:serializerTYPE REF TO cl_trex_json_serializer,
deserializerTYPE REF TO cl_trex_json_deserializer,
ref_descrTYPE REF TO cl_abap_structdescr.
DATA:gv_jsonTYPE string.
FIELD-SYMBOLS:TYPE STANDARD TABLE.
START-OF-SELECTION.
PERFORM frm_sc_data."生产数据
PERFORM frm_sc_json."生产json数据
PERFORM frm_get_data."生成动态内表,
PERFORM frm_display_data."获取想要数据,显示数据
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form FRM_SC_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_sc_data.
gs_data1-index = '2'.
gs_data1-pernr= '10000002'.
gs_data1-name= '男2号'.
gs_data1-sex= '男'.
gs_data1-phone= '12568956892'.
APPEND gs_data1TO gt_data1.
gs_data1-index = '3'.
gs_data1-pernr= '10000003'.
gs_data1-name= '女3号'.
gs_data1-sex= '女'.
gs_data1-phone= '12568956893'.
APPEND gs_data1TO gt_data1.
gs_data1-index = '4'.
gs_data1-pernr= '10000004'.
gs_data1-name= '女1号'.
gs_data1-sex= '女'.
gs_data1-phone= '12568956894'.
APPEND gs_data1TO gt_data1.
gs_data1-index = '5'.
gs_data1-pernr= '10000005'.
gs_data1-name= '男5号'.
gs_data1-sex= '男'.
gs_data1-phone= '12568956895'.
APPEND gs_data1TO gt_data1.
gs_data1-index = '6'.
gs_data1-pernr= '10000006'.
gs_data1-name= '男6号'.
gs_data1-sex= '男'.
gs_data1-phone= '12568956896'.
APPEND gs_data1TO gt_data1.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SC_DYN_TAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_sc_json.
CREATE OBJECT serializer
EXPORTING
data = gt_data1[].
serializer->serialize( ).
gv_json= serializer->get_data( ).
WRITE: / gv_json.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_get_data.
DATA:dyn_tableTYPE REF TO data.
DATA:lv_p1TYPE i,
lv_p2TYPE i,
lv_lTYPE i,
lv_textTYPE string,
lv_text1TYPE string,
lv_lenTYPE i,
lv_fieldTYPE string,
lv_indexTYPE i.
SEARCH gv_jsonFOR '{'.
IF sy-subrc= 0.
lv_p1= sy-fdpos +1.
ENDIF.
SEARCH gv_jsonFOR '}'.
IF sy-subrc= 0.
lv_p2= sy-fdpos.
ENDIF.
lv_l= lv_p2- lv_p1.
lv_text= gv_json+lv_p1(lv_l).
WRITE:/ lv_text.
DO.
CLEAR:lv_p1,lv_p2,lv_len.
lv_len= strlen( lv_text).
SEARCH lv_textFOR '"'.
IF sy-subrc= 0.
lv_p1= sy-fdpos.
ENDIF.
SEARCH lv_textFOR ','.
IF sy-subrc= 0.
lv_p2= sy-fdpos.
ENDIF.
IF lv_p2IS INITIAL.
lv_l= lv_len- lv_p1.
ELSE.
lv_l= lv_p2- lv_p1 +1.
ENDIF.
IF lv_p1IS INITIAL.
EXIT.
ENDIF.
lv_text1= lv_text+lv_p1(lv_l).
REPLACE lv_text1WITH '' INTO lv_text.
ENDDO.
WRITE:/ lv_text.
CONDENSE lv_textNO-GAPS.
DO.
lv_index= sy-index.
SPLIT lv_textAT ':' INTO lv_field lv_text.
IF sy-subrc= 0.
gs_fcat-fieldname= lv_field.
gs_fcat-col_pos= lv_index.
gs_fcat-inttype= 'g'.
* gs_fcat-intlen = 8.
* gs_fcat-scrtext_l = '描述'."加上以后可直接用于ALV显示所需结构
APPEND gs_fcatTO gt_fcat.
ELSE.
EXIT.
ENDIF.
IF lv_textIS INITIAL."当字符串为空时,拆分字符sy-subrc= 0
EXIT.
ENDIF.
ENDDO.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog= gt_fcat
IMPORTING
ep_table= dyn_table.
ASSIGN dyn_table->*TO .
* CREATE DATA dyn_line LIKE LINE OF <dyn_tab>."可生产结构
* ASSIGN dyn_line->* TO <dyn_line>.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_display_data.
CREATE OBJECT deserializer.
deserializer->deserialize(
EXPORTING json= gv_json
IMPORTING abap= ).
MOVE-CORRESPONDING TO gt_data2.
LOOP AT gt_data2INTO gs_data2.
WRITE:/ gs_data2-index,'-',gs_data2-pernr,'-',gs_data2-phone.
ENDLOOP.
ENDFORM.
、、、
bat获取json文件字段要将数据转化成字段。将数据转为json字符串格式,再转json对象,直接拿json下的字段名来获取字段值,这里要将userData也转为json对象,就可以拿到需要的字段值。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)