JSON :是一种简单的数据交换格式
格式:
{
"structure": [
{"name":"jim","age":23},
{"name":"lily","年龄":24}
]
}
一个对象以“{”(左括号)开始,“}”(右括号)结束
一个数组以“[”(左中括号)开始,“]”(右中括号)结束
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: lt_zxlth TYPE STANDARD TABLE OF zxlth.
SELECT * FROM zxlth INTO TABLE lt_zxlth UP TO 10 ROWS.
*内表-> JSON
CREATE OBJECT lo_json_ser
EXPORTING
data = lt_zxlth[].
CALL METHOD lo_json_ser->serialize.
CALL METHOD lo_json_ser->get_data
RECEIVING
rval = json_string.
*WRITE:/ json_string.
CLEAR:lt_zxlth[].
*JSON->内表
CREATE OBJECT lo_json_des.
CALL METHOD lo_json_des->deserialize
EXPORTING
json = json_string
IMPORTING
abap = lt_zxlth[].
WRITE:123.
注意:该种转换方式,生成的JSON,KEY关键字没有引号,字符串中包含空格,如果与第三方系统做接口时,就会因为JSON格式问题,导致接口失败。
这个时候,需要参照这2个类新建2个对应的新类 zcl_trex_json_serializer、zcl_trex_json_deserializer 来转换的时候加上引号
zcl_trex_json_serializer->RECURSE 加上如下图所示
CONCATENATE
CONCATENATE '"' -name '"' c_colon INTO l_value.
zcl_trex_json_deserializer->DESERIALIZE_OBJECT
图片来源:https://archive.sap.com/image/665574
2. CALL TRANSFORMATION id
SELECT * FROM zxlth INTO TABLE lt_zxlth UP TO 10 ROWS.
* ABAP to JSON
lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id
SOURCE text = lt_zxlth[]
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_zxlth.
WRITE:123.
注意:该种JSON转换生成的数据,比内表数据多了一部分“TEXT”字符串,也不是很满意
3. /UI2/CL_JSON方式(旧系统可能没有这个类)
DATA: lt_mara TYPE STANDARD TABLE OF mara,
lrf_descr TYPE REF TO cl_abap_typedescr,
lv_json TYPE string.
SELECT * FROM mara INTO TABLE lt_mara UP TO 5 ROWS.
* serialize table lt_flight into JSON, skipping initial fields and converting ABAP field names into camelCase
lv_json = /ui2/cl_json=>serialize(
data = lt_mara
compress = abap_true
pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
WRITE / lv_json.
CLEAR lt_mara.
* 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_mara ).
* serialize ABAP object into JSON string
lrf_descr = cl_abap_typedescr=>describe_by_data( lt_mara ).
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 'field' IN post_string WITH 'Field'.