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 ...
随机推荐
- linux权限与系统信息
权限 1.权限分为3个部分 可读(r) 可写(w) 可执行(x) 没有对应权限(-) 2.权限位 权限位主要分为三个部分,分别是属主.属组以及其他人 rwx : 属主 r-x : 属组 r-x : 其 ...
- 想减少代码量,快设置一个有感知的 Aware Spring Bean
摘要:正常情况下,Spring 中的 Bean 对 Spring 是无感知的,Spring 框架提供了这种扩展能力,能让一个 bean 成为有感知的. 本文分享自华为云社区<有感知的 Aware ...
- QT-图标设置
记录一下怎么在QT里添加图片,包括exe图标和里面使用的其他图片. 注意这个图片是指ico后缀的图片,去网上搜就有了,一大堆. 随便找的一个在线转换,http://www.bitbug.net/ 新建 ...
- Java之万年历
@(文章目录) 二.Java之万年历 2.1 要求 输入年份: 输入月份: 输出某年某月的日历. 2.2 思路 实现从控制台接收年和月,判断是否是闰年(判断是否是闰年:能被4整除但不能被100整除:或 ...
- 1.Docker容器学习之新生入门必备基础知识
0x00 Docker 快速入门 1.基础介绍 描述:Docker [ˈdɑ:kə(r)] 是一个基于Go语言开发实现的遵循Apache 2.0协议开源项目,目标是实现轻量级的操作系统虚拟化解决方案: ...
- Attention Mechanism in Computer Vision
前言 本文系统全面地介绍了Attention机制的不同类别,介绍了每个类别的原理.优缺点. 欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结.最新技术跟踪.经典论文解读.CV招聘信息. 概 ...
- python基础练习题(题目 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少)
day13 --------------------------------------------------------------- 实例021:猴子偷桃 题目 猴子吃桃问题:猴子第一天摘下若干 ...
- CA周记 - 带你进⼊ OpenAI 的世界
2021年11月的 Microsoft Ignite , 微软带来了全新的 Azure OpenAI Service,通过新的 Azure 认知服务能够访问 OpenAI 强大的 GPT-3 模型 . ...
- shiro550反序列学习
Shiro550 shiro550和fastjson作为攻防演练的利器,前面学习了fastjson的相关利用和回显,本篇主要来学习一下shiro550的漏洞原理. 1.漏洞原因 在 Shiro < ...
- Unity实现A*寻路算法学习2.0
二叉树存储路径节点 1.0中虽然实现了寻路的算法,但是使用List<>来保存节点性能并不够强 寻路算法学习1.0在这里:https://www.cnblogs.com/AlphaIcaru ...