/ui2/cl_json=>deserialize 无法解析嵌套的 JSON 对象

huangapple go评论140阅读模式
英文:

/ui2/cl_json=>deserialize does not parse nested json object

问题

我正在尝试解析一个嵌套的 JSON 对象作为字符串,使用 ABAP 中的 /ui2/cl_json 类...

编辑我找到了解决方案,我只需要一行代码来定义类型,这恰好是我的表类型:

DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.

然后我可以安全地将该类型传递给我的 json 反序列化函数,它也可以处理嵌套字段...
重要提示:你必须为你的 .include 给一个组名,在我的情况下是“address”,以将其识别为名为“address”的子结构...

{
   "id":1,
   "uuid":"3423e00f-b5c2-4f2c-bf88-baceca11c5f4",
   "firstName":"Isabel",
   ...
   "companyId":20
}

编辑:我替换了先前的图像。现在填充了地址对象的字段。然而,我不知道如何将这个结构(带有子地址对象)插入到我的用户字典表中(因为表似乎只允许有扁平字段..)。
我将我的“address”结构作为“.INCLUDE”字段包含到我的表中,但它没有被识别为“address”字段对象,所以我可能需要重写我的结构以适应我的表结构,并逐行插入..

/ui2/cl_json=>deserialize 无法解析嵌套的 JSON 对象

这是我的 ABAP 报告代码:

TYPES: begin of address,
    country TYPE char30,
    county type char30,
    ...
    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,
         ...
         maybe type  char30,
         address type address,
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) = `[{"id":1,"uuid":"3423e00f-b5c2-4f2c-bf88-baceca11c5f4","firstName":... }]`

/ui2/cl_json=>deserialize(
  EXPORTING
    json             = lv_json
    pretty_name      = /ui2/cl_json=>pretty_mode-camel_case
  CHANGING
    data             = lt_user
).

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.

我不知道在 /ui2/cl_json 类中是否有更好的反序列化/解析选项,或者如何以其他方式做到这一点... 有什么想法吗?

英文:

I am trying to parse a nested json object as string with the class /ui2/cl_json in ABAP...
However, it seems it does not get all the data parsed from the json:

EDIT: I found the solution, I just neede a single line to define the type which is exactly my table type:

DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.

I then can safely pass that type to my json deserialize function and it simply works also with nested fields....
IMPORTANT: you have to give your .include a group name, in my case "address" to recognize it as a named "address" substructure...

{
   "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
}

EDIT: I replaced the previous image. The fields for address object are filled now. However I don't know how to insert that structure (with sub-address object) into my dictionary table of users (because tables are allowed to have only flat fields as it seems..)
I included my "address" structure as ".INCLUDE" field into my table, but it's not recognized as the "address" field object, so I probably would have to rewrite my structures to fit my table structure and insert line by line..

/ui2/cl_json=>deserialize 无法解析嵌套的 JSON 对象

here is my abap report code:

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.

I dont know if there are any better deserialize / parse options in the /ui2/cl_json class or how to do it otherwise... any ideas?

答案1

得分: 1

看到我上面的编辑...

DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.

已经足够声明我的表类型并将其传递给我的 JSON 反序列化函数作为输入... 就是这样!
而且别忘了.... 你必须给你的 .include 一个组名,在我的情况下是 "address",以识别它作为一个名为 "address" 的子结构...

英文:

See my edits above...

DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.

was enough to declare my table type and to pass it down to my json deserialize function as import... and thats it!
And don't forget.... you have to give your .include a group name, in my case "address" to recognize it as a named "address" substructure...

huangapple
  • 本文由 发表于 2023年3月20日 23:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定