一、JSON概念
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
JSON数据格式示例:
{
"学生": [
{"姓名":"小明","年龄":23},
{"姓名":"大憨","年龄":24}
]
}
复制代码
一个对象以“{”(左括号)开始,“}”(右括号)结束
一个数组以“[”(左中括号)开始,“]”(右中括号)结束
二、JSON与XML
XML数据格式:
复制代码
编码的可读性,xml有明显的优势,毕竟人类的语言更贴近这样的说明结构。json读起来更像一个数据块,读起来就比较费解了。不过,我们读起来费解的语言,恰恰是适合机器阅读。
JSON难阅读,试试:JSON在线编辑器
JSON没有多余的标签,所以JSON比xml网络传输效率更高。
三、JSON与ABAP格式互转
1、cl_trex_json_serializer、cl_trex_json_deserializer方式
DATA: lo_json_ser TYPE REF TO cl_trex_json_serializer,
lo_json_des TYPE REF TO cl_trex_json_deserializer.
DATA: json_string TYPE string.
DATA:BEGIN OF lt_spfli OCCURS 0,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
cityfrom TYPE spfli-cityfrom,
countryto TYPE spfli-countryto,
END OF lt_spfli.
START-OF-SELECTION.
SELECT
carrid
connid
cityfrom
countryto
UP TO 10 ROWS
INTO TABLE lt_spfli
FROM spfli.
*内表->JSON
CREATE OBJECT lo_json_ser
EXPORTING
data = lt_spfli[].
CALL METHOD lo_json_ser->serialize.
CALL METHOD lo_json_ser->get_data
RECEIVING
rval = json_string.
WRITE:json_string.
CLEAR:lt_spfli[].
*JSON->内表
CREATE OBJECT lo_json_des.
CALL METHOD lo_json_des->deserialize
EXPORTING
json = json_string
IMPORTING
abap = lt_spfli[].
复制代码
该种转换方式,生成的JSON,KEY关键字没有引号,字符串中包含空格,如果与第三方系统做接口时,就会因为JSON格式问题,导致接口失败。
2、CALL TRANSFORMATION id 方式
DATA:BEGIN OF lt_spfli OCCURS 0,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
cityfrom TYPE spfli-cityfrom,
countryto TYPE spfli-countryto,
END OF lt_spfli.
DATA: lo_writer TYPE REF TO cl_sxml_string_writer,
lv_output_length TYPE i,
lt_binary_tab TYPE STANDARD TABLE OF sdokcntbin,
lv_jsonx TYPE xstring,
lv_json TYPE string.
SELECT carrid
connid
cityfrom
countryto
UP TO 10 ROWS
INTO TABLE lt_spfli
FROM spfli.
* ABAP to JSON
lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE text = lt_spfli[] RESULT XML lo_writer.
lv_jsonx = lo_writer->get_output( ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_jsonx
IMPORTING
output_length = lv_output_length
TABLES
binary_tab = lt_binary_tab.
CALL FUNCTION 'SCMS_BINARY_TO_STRING'
EXPORTING
input_length = lv_output_length
IMPORTING
text_buffer = lv_json
output_length = lv_output_length
TABLES
binary_tab = lt_binary_tab.
* JSON to ABAP
CALL TRANSFORMATION id SOURCE XML lv_jsonx RESULT text = lt_spfli.
复制代码
该种JSON转换生成的数据,比内表数据多了一部分“TEXT”字符串,也不是很满意
3、/UI2/CL_JSON方式
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.
复制代码
/UI2/CL_JSON方式转换生成的JSON格式就完美了,他不仅支持结构、内表转换成JSON,还支持嵌套结构deep structure这种复杂的结构,了解更多请参考:
https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
注意:ABAP中定义变量是不区分大小写,而JAVA中变量是区分大小写的,所以需要对字符串替换一下,如下:
REPLACE ALL OCCURRENCES OF 'contactname' IN post_string WITH 'contactName'.
复制代码