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 ...
随机推荐
- MVC:开发模式&&三层架构:软件设计架构
MVC:开发模式 jsp演变历史 早期只有servlet,只能使用response输出标签数据,非常麻烦 后来又jsp,简化了Servlet的开发,如果过度使用jsp,在jsp中即写大量的java代码 ...
- [Where is My Chicken Soup] 鸡汤来咯
20岁的年纪安逸不是好的选择.尝试突破自己,你的本事不止你现在的样子. 保持适当的焦虑,它会一直鞭策你进步. 失败的原因有很多,并不代表你真的不行.但如果自己真就这么放弃了,那你才是真的不行.
- android软件简约记账app开发day06-将记账条目添加到数据库并且绘制备注页面
android软件简约记账app开发day06-将记账条目添加到数据库并且绘制备注页面 首先写添加到数据库 在DBOpenHelper中添加创建记账表的语句 //创建记账表 sql = "c ...
- Envoy熔断限流实践(一)基于Rainbond插件实现熔断
Envoy 可以作为 Sevice Mesh 微服务框架中的代理实现方案,Rainbond 内置的微服务框架同样基于 Envoy 实现.本文所描述的熔断实践基于 Rainbond 特有的插件机制实现. ...
- IOC容器--1.12. 基于 Java 的容器配置
用Java的方式配置Spring ,不使用Spring的XML配置,全权交给Java来做 JavaConfig是Spring的一个子项目,在Sring 4 之后成为核心功能 这种纯Java的配置方式 ...
- 推荐三个好用的TamperMonkey洛谷插件
Part 1: TamperMonkey 插件 Part 1.1 什么是 Tampermonkey 在我们学习的过程中,往往想要更多功能,这时候可以使用 TamperMonkey 插件进行美化 官网介 ...
- OAuth2授权服务器Id Server一键生成配置原理
OAuth2客户端的配置参数非常多,虽然Id Server通过控制台可视化解决了创建OAuth2客户端的问题.但是如何进一步降低OAuth2的使用难度,把创建的OAuth2客户端转化为配置成为了刚需, ...
- React 与 Hooks 如何使用 TypeScript 书写类型?
React 与 Hooks 如何使用 TypeScript 书写类型? 本文写于 2020 年 9 月 20 日 函数组件与 TS 对于 Hooks 来说是不支持使用 class 组件的. 如何在函数 ...
- 1903021121-刘明伟-java十一周作业-java面向对象编程
项目 内容 课程班级博客链接 19级信计班(本) 作业要求链接 第十一周作业 博客名称 1903021121-刘明伟-java十一周作业-java面向对象 要求 每道题要有题目,代码(使用插入代码,不 ...
- 用crash tool观察ARM64 Linux地址转换
初学者学习Linux系统地址转换时,如果只是学习理论,又或者研读代码,那可能感觉比较枯燥.此时如果可以利用某些工具实际观察一下地址转换的过程,那可能会给枯燥的内核学习带来些微的乐趣.crash too ...