我试图在ABAP中使用类/ui 2/cl_json将嵌套的json对象解析为字符串......然而,它似乎没有从json中解析出所有数据:
编辑:我找到了解决方案,我只尼德一行来定义类型,这正是我的表类型:
DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.
**然后我可以安全地将该类型传递给我的json反序列化函数,它也可以简单地处理嵌套字段....**重要信息:你必须给予你的.include一个组名,在我的例子中是“地址”,以识别它作为一个命名的“地址”子结构...
{
"id":1,
"uuid":"3423e00f-b5c2-4f2c-bf88-baceca11c5f4",
"firstName":"Isabel",
"lastName":"Zapletal",
"fullName":"Eddi Engel",
"gender":"männlich",
"username":"Annabelle12",
"email":"Mick87@hotmail.com",
"avatar":"https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/221.jpg",
"password":"4GYJ5LYoXrEmpZU",
"birthdate":"30.4.1988",
"registeredAt":"2023-02-26T10:49:56.165Z",
"phone":"97-1365-5739",
"jobTitle":"Corporate Brand Executive",
"jobType":"Agent",
"profileInfo":"Hi, my name is Bo Neuendorf.\n I was born in Tue May 14 1968 03:04:13 GMT+0100 (Mitteleuropäische Sommerzeit) and I am currently working as a Chief Branding Officer at Rossberg Gruppe.\n Check out my site on exzessiv-erwachsener.ch and contact me any time at +79 799 514 8382. Vitae unde tempora dolore a magnam. Consequatur deleniti veniam unde porro voluptates harum exercitationem cum reprehenderit. Eos ullam dignissimos laborum veniam voluptas consequuntur. Deleniti tempora sed veritatis ipsam laborum blanditiis. Vero vitae distinctio aut ea nihil. Soluta unde inventore.",
"address":{
"country":"Kroatien",
"county":"Buckinghamshire",
"city":"Neu Giuliadorf",
"streetAddress":"Am Quettinger Feld 977 Zimmer 152",
"latitude":7.6716,
"longitude":-70.7275,
"coordinates":[
83.8491,
-96.1326
],
"longLat":[
1.09844,
41.6314
],
"timezone":"America/Sao_Paulo",
"zipCode":"99171"
},
"maybe":"Yeah I'm here!",
"products":[
85,
8,
15
],
"companyId":20
}
编辑:我替换了以前的图像。地址对象的字段现在被填充了。但是我不知道如何插入该结构(带子地址对象)到我的用户字典表中(因为表看起来只允许有平面字段..)我将我的“address”结构作为“.INCLUDE”字段包含到我的表中,但它不被识别为“address”字段对象,因此,我可能必须重写我的结构,以适应我的表结构,并逐行插入。
下面是我ABAP报告代码:
TYPES: begin of address,
country TYPE char30,
county type char30,
city type char50,
streetAddress type string,
latitude type dec015,
longitude type dec015,
* coordinates TYPE ty_coord,
* longLat TYPE ty_coord,
* timezone type char50,
zipcode type int4,
END of address.
TYPES: BEGIN OF ls_user,
mandt TYPE mandt,
id TYPE num05_kk2,
uuid TYPE SYSUUID_C36,
firstName TYPE char30,
lastName type char30,
fullName type char50,
gender type char10,
username type char30,
email type AD_SMTPADR,
avatar type string,
password type char50,
birthdate type char10,
registeredAt type char30,
phone type char30,
jobTitle type char50,
jobType type char30,
profileInfo type string,
address type address,
* country TYPE zalm_fs_address-country,
* county type zalm_fs_address-county,
* city type zalm_fs_address-city,
* streetAddress type zalm_fs_address-streetaddress,
* latitude type zalm_fs_address-latitude,
* longitude type zalm_fs_address-longitude,
** coordinates TYPE ty_coord,
** longLat TYPE ty_coord,
** timezone type char50,
* zipcode type zalm_fs_address-zipcode,
maybe type char30,
* products Type STANDARD TABLE OF int4 with non-UNIQUE default key,
* companyId: type char5,
END OF ls_user.
DATA lv_dauer TYPE i.
DATA lt_user TYPE STANDARD TABLE OF ls_user WITH NON-UNIQUE DEFAULT KEY.
DATA wa_user TYPE ls_user.
*DATA: lv_json TYPE string.
DATA(lv_json) =
`[{"id":1,"uuid":"3423e00f-b5c2-4f2c-bf88-baceca11c5f4","firstName":... }]`
* JSON -> ABAP (iTab)
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_json
* jsonx =
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
* assoc_arrays =
* assoc_arrays_opt =
* name_mappings =
* conversion_exits =
* hex_as_base64 =
CHANGING
data = lt_user
).
**cl_demo_output=>write_data( lv_json ).
*cl_demo_output=>write_data( lt_user[ 4 ]-address ).
**
*cl_demo_output=>write_data( lt_user[ 5 ]-jobtitle ).
*cl_demo_output=>display( lt_user[ 6 ]-profileinfo ).
DELETE FROM zalm_fs_users.
get run time field lv_dauer.
INSERT zalm_fs_users FROM TABLE lt_user. "ACCEPTING DUPLICATE KEYS.
get run time field lv_dauer.
Write lv_dauer.
我不知道在/ui 2/cl_json类中是否有更好的反序列化/解析选项,或者如何做到这一点...任何想法?
1条答案
按热度按时间wn9m85ua1#
看到我的编辑以上...
就足以声明我的表类型,并将其作为import传递给我的json反序列化函数......就是这样!不要忘记......你必须给予你的**.include**一个组名,在我的例子中是“address”,以识别它作为一个命名的“address”子结构......