SAP JSON 格式化及解析。
一、首选:/ui2/cl_json {'key':'value'}
/ui2/cl_json=>deserialize( EXPORTING json = json
CHANGING data = data ).
二、cl_trex_json_serializer ;lcl_trex_json_deserializer 改造版。 {'key':'value'}
REPORT zjw_test.
"多层结构内表 格式化为JSON 格式
CLASS lcl_trex_json_serializer DEFINITION DEFERRED.
"JSON 格式 转多层内表
CLASS lcl_trex_json_deserializer DEFINITION DEFERRED.
CLASS lcl_trex_json_serializer DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:class_constructor.
METHODS:
constructor
IMPORTING !data TYPE data ,
serialize,
get_data RETURNING
VALUE(rval) TYPE string.
PRIVATE SECTION.
DATA:
fragments TYPE trext_string,
data_ref TYPE REF TO data.
CLASS-DATA: c_colon TYPE string.
CLASS-DATA: c_comma TYPE string.
METHODS: recurse
IMPORTING
!data TYPE data .
ENDCLASS.
CLASS lcl_trex_json_deserializer DEFINITION.
PUBLIC SECTION.
*"* public components of class CL_TREX_JSON_DESERIALIZER
*"* do not include other source files here!!!
INTERFACES if_trex_serialization .
METHODS deserialize
IMPORTING
!json TYPE string
EXPORTING
!abap TYPE any .
METHODS deserialize_ref
IMPORTING
!json TYPE string
!ref TYPE REF TO object .
PRIVATE SECTION.
*"* private components of class CL_TREX_JSON_DESERIALIZER
*"* do not include other source files here!!!
METHODS deserialize_node
IMPORTING
!json TYPE string
CHANGING
!offset TYPE i DEFAULT 0
!node TYPE any
RAISING
cx_trex_serialization .
METHODS deserialize_object
IMPORTING
!json TYPE string
CHANGING
!offset TYPE i DEFAULT 0
!node TYPE any
RAISING
cx_trex_serialization .
METHODS deserialize_array
IMPORTING
!json TYPE string
CHANGING
!offset TYPE i DEFAULT 0
!node TYPE any
RAISING
cx_trex_serialization .
ENDCLASS.
CLASS lcl_trex_json_deserializer IMPLEMENTATION.
METHOD deserialize_array.
DATA:
l_done TYPE abap_bool,
l_rec TYPE REF TO data.
FIELD-SYMBOLS:
<itab> TYPE ANY TABLE,
<rec> TYPE data.
ADD 1 TO offset . "skip [
ASSIGN node TO <itab> .
* create record
CREATE DATA l_rec LIKE LINE OF <itab> .
ASSIGN l_rec->* TO <rec> .
WHILE l_done = abap_false .
CLEAR <rec> .
deserialize_node(
EXPORTING
json = json
CHANGING
offset = offset
node = <rec> ) .
INSERT <rec> INTO TABLE <itab> .
FIND REGEX ',|\]' IN SECTION OFFSET offset OF json MATCH OFFSET offset .
IF sy-subrc <> 0 .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDIF .
IF json+offset(1) = ']' .
l_done = abap_true .
ENDIF .
ADD 1 TO offset .
ENDWHILE .
ENDMETHOD.
METHOD deserialize.
deserialize_node(
EXPORTING
json = json
CHANGING
node = abap ) .
ENDMETHOD.
METHOD deserialize_node.
DATA:
l_len TYPE i,
l_string TYPE string,
l_number TYPE string.
FIND REGEX '\{|\[|"([^":]*)"|(\d+)' IN SECTION OFFSET offset OF json "changed by jw
MATCH OFFSET offset MATCH LENGTH l_len
SUBMATCHES l_string l_number .
IF sy-subrc <> 0 .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDIF .
CASE json+offset(1) .
WHEN '{' .
deserialize_object(
EXPORTING
json = json
CHANGING
offset = offset
node = node ) .
WHEN '[' .
deserialize_array(
EXPORTING
json = json
CHANGING
offset = offset
node = node ) .
WHEN '"' .
node = l_string .
ADD l_len TO offset .
WHEN OTHERS . "0-9
node = l_number .
ADD l_len TO offset .
ENDCASE .
ENDMETHOD.
METHOD deserialize_ref.
DATA l_ref TYPE REF TO object .
l_ref = ref .
deserialize_node(
EXPORTING
json = json
CHANGING
node = l_ref ) .
ENDMETHOD.
METHOD deserialize_object.
DATA:
l_node_type TYPE REF TO cl_abap_typedescr,
l_ref TYPE REF TO object.
ADD 1 TO offset . "skip {
l_node_type = cl_abap_typedescr=>describe_by_data( node ) .
* prepare for dynamic access
CASE l_node_type->kind .
WHEN cl_abap_typedescr=>kind_ref .
l_ref = node .
WHEN cl_abap_typedescr=>kind_struct .
WHEN OTHERS .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDCASE .
DATA:
l_done TYPE abap_bool,
l_len TYPE i,
l_name TYPE string.
* handle each component
WHILE l_done = abap_false .
"find next key
FIND REGEX '(\w+)([^":]*)\s*' IN SECTION OFFSET offset OF json "changed by jw
MATCH OFFSET offset MATCH LENGTH l_len
SUBMATCHES l_name .
IF sy-subrc <> 0 .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDIF .
ADD l_len TO offset .
FIELD-SYMBOLS <comp> TYPE any .
* dynamic binding to component
TRANSLATE l_name TO UPPER CASE .
CASE l_node_type->kind .
WHEN cl_abap_typedescr=>kind_ref .
ASSIGN l_ref->(l_name) TO <comp> .
WHEN cl_abap_typedescr=>kind_struct .
ASSIGN COMPONENT l_name OF STRUCTURE node TO <comp> .
WHEN OTHERS .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDCASE .
DATA:
l_comp_type TYPE REF TO cl_abap_typedescr,
l_ref_type TYPE REF TO cl_abap_refdescr.
* check component type
l_comp_type = cl_abap_typedescr=>describe_by_data( <comp> ) .
CASE l_comp_type->kind .
* create instance if it's an oref
WHEN cl_abap_typedescr=>kind_ref .
l_ref_type ?= l_comp_type .
l_comp_type = l_ref_type->get_referenced_type( ) .
CREATE OBJECT <comp> TYPE (l_comp_type->absolute_name) .
ENDCASE .
* deserialize current component
deserialize_node(
EXPORTING
json = json
CHANGING
offset = offset
node = <comp> ) .
FIND REGEX ',|\}' IN SECTION OFFSET offset OF json MATCH OFFSET offset .
IF sy-subrc <> 0 .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDIF .
IF json+offset(1) = '}' .
l_done = abap_true .
ENDIF .
ADD 1 TO offset .
ENDWHILE .
ENDMETHOD.
ENDCLASS.
CLASS lcl_trex_json_serializer IMPLEMENTATION.
"CLASS_CONSTRUCTOR
METHOD class_constructor. cl_abap_string_utilities
=>c2str_preserving_blanks(
EXPORTING source = ': '
IMPORTING dest = c_colon ) . cl_abap_string_utilities
=>c2str_preserving_blanks(
EXPORTING source = ', '
IMPORTING dest = c_comma ) .
ENDMETHOD.
METHOD constructor.
GET REFERENCE OF data INTO me->data_ref .
ENDMETHOD.
METHOD get_data.
CONCATENATE LINES OF me->fragments INTO rval .
ENDMETHOD.
METHOD serialize.
FIELD-SYMBOLS <data> TYPE data .
ASSIGN me->data_ref->* TO <data> . recurse
( <data> ) .
ENDMETHOD.
METHOD recurse.
DATA: l_type
TYPE c, l_comps
TYPE i, l_lines
TYPE i, l_index
TYPE i, l_value
TYPE string.
FIELD-SYMBOLS: <itab>
TYPE ANY TABLE, <comp>
TYPE any.
DESCRIBE FIELD data TYPE l_type COMPONENTS l_comps .
IF l_type = cl_abap_typedescr=>typekind_table .
* itab -> array
APPEND '[' TO me->fragments .
ASSIGN data TO <itab> . l_lines
= lines( <itab> ) .
LOOP AT <itab> ASSIGNING <comp> .
ADD 1 TO l_index . recurse
( <comp> ) .
IF l_index < l_lines .
APPEND c_comma TO me->fragments .
ENDIF .
ENDLOOP .
APPEND ']' TO fragments .
ELSE .
IF l_comps IS INITIAL .
* field -> scalar
* todo: format l_value
= data .
REPLACE ALL OCCURRENCES OF '\' IN l_value WITH '\\' .
REPLACE ALL OCCURRENCES OF '''' IN l_value WITH '\''' .
REPLACE ALL OCCURRENCES OF '"' IN l_value WITH '\"' .
REPLACE ALL OCCURRENCES OF '&' IN l_value WITH '\&' .
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf IN l_value WITH '\r\n' .
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>newline IN l_value WITH '\n' .
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN l_value WITH '\t' .
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>backspace IN l_value WITH '\b' .
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>form_feed IN l_value WITH '\f' .
CONCATENATE '"' l_value '"' INTO l_value .
APPEND l_value TO me->fragments .
ELSE .
* structure -> object
DATA l_typedescr TYPE REF TO cl_abap_structdescr .
FIELD-SYMBOLS <abapcomp> TYPE abap_compdescr .
APPEND '{' TO me->fragments . l_typedescr ?= cl_abap_typedescr
=>describe_by_data( data ) .
LOOP AT l_typedescr->components ASSIGNING <abapcomp> . l_index
= sy-tabix .
CONCATENATE '"' <abapcomp>-name '"' c_colon INTO l_value."changed by jw
TRANSLATE l_value TO LOWER CASE .
APPEND l_value TO me->fragments .
ASSIGN COMPONENT <abapcomp>-name OF STRUCTURE data TO <comp> . recurse
( <comp> ) .
IF l_index < l_comps .
APPEND c_comma TO me->fragments .
ENDIF .
ENDLOOP .
APPEND '}' TO me->fragments .
ENDIF .
ENDIF .
ENDMETHOD.
ENDCLASS.
DATA: go_serializer
TYPE REF TO lcl_trex_json_serializer, go_dserializer
TYPE REF TO lcl_trex_json_deserializer, g_json
TYPE string.
TYPES:BEGIN OF ty_out,
key TYPE char2,
value TYPE string,
END OF ty_out.
DATA:gt_out TYPE TABLE OF ty_out, gs_out
TYPE ty_out.
TYPES:ty_det LIKE TABLE OF gs_out.
DATA:BEGIN OF gs_all, ex
TYPE char2, item
TYPE ty_det,
END OF gs_all, gt_all
LIKE TABLE OF gs_all, gt_dout
LIKE TABLE OF gs_all.
START-OF-SELECTION.
APPEND INITIAL LINE TO gt_out ASSIGNING FIELD-SYMBOL(<lfs_out>). <lfs_out>
-key = '1'. <lfs_out>
-value = 'text1'.
APPEND INITIAL LINE TO gt_out ASSIGNING <lfs_out>. <lfs_out>
-key = '2'. <lfs_out>
-value = 'text2'.
APPEND INITIAL LINE TO gt_all ASSIGNING FIELD-SYMBOL(<lfs_all>). <lfs_all>
-ex = '3'. <lfs_all>
-item = gt_out.
CREATE OBJECT go_serializer
EXPORTING
data = gt_all.
CREATE OBJECT go_dserializer. go_serializer
->serialize( ). g_json
= go_serializer->get_data( ).
IF sy-subrc = 0. go_dserializer
->deserialize(
EXPORTING json = g_json
IMPORTING abap = gt_dout ).
WRITE: g_json.
ENDIF.
SAP JSON 格式化及解析。的更多相关文章
- yformater - chrome谷歌浏览器json格式化json高亮json解析插件
yformater是一款chrome浏览器插件,用来格式化(高亮)服务端接口返回的json数据. 实际上小菜并不是第一个写这种插件的,但是现有的chrome json格式化插件实在是不太好用,索性小菜 ...
- asp.net中json格式化及在js中解析json
类: public class UploadDocumentItem { public UploadDocumentItem() { } public string DocMuid { get; se ...
- DELPHI解析JSON格式化的日期
DELPHI解析JSON格式化的日期 json返回的日期是 /Date(1560355200000)/ 这样的格式. 这个1560355200000,是指1970年以后的秒数. DELPHI如何解析这 ...
- xml 和json 数据格式及解析
来源:http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”.任何技术都是为了满足人的生产生活需要而产生的.具体到小小的一个手机,里面蕴含的 ...
- ABP入门系列(8)——Json格式化
ABP入门系列目录--学习Abp框架之实操演练 讲完了分页功能,这一节我们先不急着实现新的功能.来简要介绍下Abp中Json的用法.为什么要在这一节讲呢?当然是做铺垫啊,后面的系列文章会经常和Json ...
- 校验字符串是否是JSON格式,将不规则展示的json格式的字符串进行规则展示(json格式化)
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] var str = {"code": "","svcname" ...
- Python -- Json 数据编码及解析
Python -- Json 数据编码及解析 Json 简单介绍 JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本 ...
- iOS - - JSON 和 XML解析
JSON 和 XML 一.JSON 1.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) 2.JSON的格 ...
- golang结构体json格式化的时间格式
golang结构体json格式化的时间格式 在我们开发中,经常会解析time.Time 往往前台传过来的时候,是个string 但是我们希望在结构体转成time.Time type Param str ...
随机推荐
- Gitlab图床配置
注意,使用图床,如果文件在外网打开,图片不会正常显示,因为图片存储在内部的Gitlab服务器上 自行搜索Picgo安装配置,需要安装node.js 项目链接:D-W-X/picgo-plugin- ...
- Go Slice Tricks Cheat Sheet、Go 切片使用小妙招
AppendVector. Copy. Cut. Delete. Delete without preserving order. Cut (GC). Delete (GC). Delete with ...
- docker基础_数据卷
docker数据卷 为什么要使用数据卷 如果数据都在容器中,那么容器一旦删除,数据就会丢失!docker容器需要将产生的数据同步到本地.容器与容器之间也需要有一个数据共享的技术 将某些文件共享.这就是 ...
- 基于深度学习的车辆检测系统(MATLAB代码,含GUI界面)
摘要:当前深度学习在目标检测领域的影响日益显著,本文主要基于深度学习的目标检测算法实现车辆检测,为大家介绍如何利用\(\color{#4285f4}{M}\color{#ea4335}{A}\colo ...
- Ansible Notes: Tower Credential的本质
Ansible AWX/Tower credential 的本质 Ansible Tower (社区版本叫AWX)用credential这个资源来对象来存储playbook运行过程中用到的机密信息.比 ...
- XCTF练习题---MISC---stegano
XCTF练习题---MISC---stegano flag:flag{1nv151bl3m3554g3} 解题步骤: 1.观察题目,下载附件 2.打开发现是一张PDF图片,尝试转换word无果后,想到 ...
- B+树能存多少数据?
B+树能存多少数据? 图 MySQL B+树示意图 InnoDB页的大小默认是16KB: 假设一条记录大小为1KB,则一个数据页中可以存16条数据(忽略页中的其他数据结构) 假设主键为int,又指针大 ...
- SpringBoot如何优雅关闭(SpringBoot2.3&Spring Boot2.2)
SpringBoot如何优雅关闭(SpringBoot2.3&Spring Boot2.2) 优雅停止&暴力停止 暴力停止:像日常开发过程中,测试区或者本地开发时,我们并不会考虑项目关 ...
- Java 15 新特性:隐藏类
什么是隐藏类 隐藏类,是一种不能被其他类直接使用的类.引入隐藏类的主要目的是给框架来使用,使得框架可以在运行时生成类,并通过反射间接使用它们.可能有点抽象,不要紧,下面我们通过一个例子来直观的认识它! ...
- k8s docker 中部署think php 并搭建php websocket
不得不说php 对云原生有点不够友好,之前用java .net打包docker镜像 一下就ok了,php倒腾了好久才算部署成功. 场景:使用阿里云ack(k8s) 部署采用thinkPHP框架的php ...