ABAP-JSON格式转换详解编程语言

转载:IT虾米网

DATA: lt_flight TYPE STANDARD TABLE OF sflight, 
      lrf_descr TYPE REF TO cl_abap_typedescr, 
      lv_json   TYPE string. 
  
   
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 = /ui2/cl_json=>serialize( data = lt_flight compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ). 
WRITE / lv_json. 
  
CLEAR lt_flight. 
   
* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping 
/ui2/cl_json=>deserialize( EXPORTING json = lv_json pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = lt_flight ). 
  
* serialize ABAP object into JSON string 
lrf_descr = cl_abap_typedescr=>describe_by_data( lt_flight ). 
lv_json = /ui2/cl_json=>serialize( lrf_descr ). 
WRITE / lv_json.
TYPES: 
  BEGIN OF tp_s_data, 
    sschema             TYPE string, 
    odatacontext        TYPE string, 
    shortened_abap_name TYPE string, 
    standard            TYPE string, 
  END OF tp_s_data. 
  
DATA: ls_exp      TYPE tp_s_data, 
      lt_mapping  TYPE /ui2/cl_json=>name_mappings, 
      lv_json     TYPE /ui2/cl_json=>json. 
  
lt_mapping = VALUE #( ( abap = `SSCHEMA` json = `$schema` ) 
                      ( abap = `ODATACONTEXT` json = [email protected]` ) 
                      ( abap = `SHORTENED_ABAP_NAME` json = `VeeeeryyyyyLooooongJSONAttrbuuuuuuuuuteeeeeeeeeee` ) ). 
  
lv_json = /ui2/cl_json=>serialize( data = ls_exp name_mappings = lt_mapping ).
TYPES: 
  BEGIN OF ts_node, 
    id        TYPE i, 
    children  TYPE STANDARD TABLE OF REF TO data WITH DEFAULT KEY, 
  END OF ts_node. 
  
DATA: lv_exp    TYPE string, 
      lv_act    TYPE string, 
      ls_data   TYPE ts_node, 
      lr_data   LIKE REF TO ls_data. 
  
ls_data-id = 1. 
  
CREATE DATA lr_data. 
lr_data->id = 2. 
APPEND lr_data TO ls_data-children.
DATA: lo_act    TYPE REF TO lcl_test, 
      lo_exp    TYPE REF TO lcl_test, 
      lv_json   TYPE string, 
      lo_child  LIKE lo_data. 
  
CREATE OBJECT lo_exp. 
  
lo_exp ->id = 1. 
  
CREATE OBJECT lo_child. 
lo_child->id = 2. 
APPEND lo_child TO lo_exp->children. 
  
lv_json = /ui2/cl_json=>serialize( data = lo_exp ). 
ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data =  lo_act ).
DATA: lv_json TYPE /ui2/cl_json=>json, 
      lr_data TYPE REF TO data. 
  
FIELD-SYMBOLS: 
  <data>   TYPE data, 
  <struct> TYPE any, 
  <field>  TYPE any. 
  
lv_json = `{"name":"Key1","properties":{"field1":"Value1","field2":"Value2"}}`. 
lr_data = /ui2/cl_json=>generate( json = lv_json ). 
  
" OK, generated, now let us access somete field :( 
IF lr_data IS BOUND. 
  ASSIGN lr_data->* TO <data>. 
  ASSIGN COMPONENT `PROPERTIES` OF STRUCTURE <data> TO <field>. 
  IF <field> IS ASSIGNED. 
    lr_data = <field>. 
    ASSIGN lr_data->* TO <data>. 
    ASSIGN COMPONENT `FIELD1` OF STRUCTURE <data> TO <field>. 
    IF <field> IS ASSIGNED. 
      lr_data = <field>. 
      ASSIGN lr_data->* TO <data>. 
      WRITE: <data>. " We got it -> Value1 
    ENDIF. 
  ENDIF. 
ENDIF.
DATA: lv_json TYPE /ui2/cl_json=>json, 
      lr_data TYPE REF TO data, 
      lv_val  TYPE string. 
  
lv_json = `{"name":"Key1","properties":{"field1":"Value1","field2":"Value2"}}`. 
lr_data = /ui2/cl_json=>generate( json = lv_json ). 
  
/ui2/cl_data_access=>create( ir_data = lr_data iv_component = `properties-field1`)->value( IMPORTING ev_data = lv_val ). 
WRITE: lv_val.
DATA: lt_flight TYPE STANDARD TABLE OF sflight, 
      lrf_descr TYPE REF TO cl_abap_typedescr, 
      lv_json   TYPE string. 
  
   
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 = /ui2/cl_json=>serialize( data = lt_flight compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ). 
WRITE / lv_json. 
  
CLEAR lt_flight. 
   
* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping 
/ui2/cl_json=>deserialize( EXPORTING json = lv_json pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = lt_flight ). 
  
* serialize ABAP object into JSON string 
lrf_descr = cl_abap_typedescr=>describe_by_data( lt_flight ). 
lv_json = /ui2/cl_json=>serialize( lrf_descr ). 
WRITE / lv_json.
TRY. 
*   将JSON转换为HTML 
    CALL TRANSFORMATION sjson2html SOURCE XML lv_json 
                                   RESULT XML DATA(lv_html). 
  CATCH cx_xslt_runtime_error INTO DATA(lo_err). 
    lv_err_text = lo_err->get_text( ). 
    WRITE: lv_err_text. 
    RETURN. 
ENDTRY. 
 
* 显示HTML 
lv_convert = cl_abap_codepage=>convert_from( lv_html ). 
cl_abap_browser=>show_html( html_string = lv_convert ).

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/17948.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论