且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

反序列化匿名 JSON 数组?

更新时间:2022-10-15 23:26:15

/UI5/CL_JSON_PARSER 解析未知格式的 JSON.

请注意,它上面多次写着供内部使用",您可能应该认真对待它并克隆其代码以修复它.

I got an anonymous array which I want to deserialize, here the example of the first array object

[
  { "time":"08:55:54",
    "date":"2016-05-27",
    "timestamp":1464332154807,
    "level":3,
    "message":"registerResourcePath ('', '/sap/bc/ui5_ui5/ui2/ushell/resources/')",
    "details":"","component":"sap.ui.ModuleSystem"},
  {"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"URL prefixes set to:","details":"","component":"sap.ui.ModuleSystem"},
  {"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"  (default) : /sap/bc/ui5_ui5/ui2/ushell/resources/","details":"","component":"sap.ui.ModuleSystem"}
]

I tried deserializing using CL_TREX_JSON_SERIALIZER, but it is corrupt and does not work with my JSON, here is why

Then I tried /UI2/CL_JSON, but it needs a "structure" that perfectly fits the object given by the JSON Object. "Structure" means in my case an internal table of objects with the attributes time, date, timestamp, level, messageanddetails. And there was the problem: it does not properly handle references and uses class description to describe the field assigned to the field-symbol. Since I can not have a list of objects but only a list of references to objects that solution also doesn't works.

As a third attempt I tried with the CALL TRANSFORMATION as described by Horst Keller, but with this method I was not able to read in an anonymous array, and here is why

My major points:

  • I do not want to change the JSON, since that is what I get from sap.ui.log
  • I prefere to use built-in functionality and not a thirdparty framework

/UI5/CL_JSON_PARSER parses JSONs with unknown format.

Note that it's got "for internal use" written on it so many times that you probably should take it seriously and clone its code to fixate it.