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, message
anddetails
. 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:
sap.ui.log
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. There are several issues, which were eliminated in the next sample: . . .
If your array continues with X4,Y4,Z4, you have a problem since, for deserializing the strong type class from the JSON, the array entries should be known. To deserialize the current JSON, use the following classes: You may read the JSON dynamically instead of deserializing the array: Alon.
Because, under the hood, this is a boxed JsonElement, a type that is the building block of native JSON DOM. So, we don’t have the convenience to use it in a truly dynamicway. In short, usingdynamicwith the native deserializer has no added benefit and results in a JSON DOM which has its own API to deal with.
One approach is to put an identifier in your JSON array string. This code worked for me: var typeExample = new { names = new[] { new { Name = "" } } }; string jsonArray = @"{ names: [{'Name':'Mike'}, {'Name':'Ben'}, {'Name':'Razvigor'}]}"; var result = JsonConvert.DeserializeAnonymousType(jsonArray, typeExample);
Your problem comes out not from the anonymity of array, but from the awkwardness of SAP JSON (De)serializer, which doesn't respect double quotes, which enclose JSON attributes. The issue is thoroughly described in this answer.
If you don't want to change your JSON on-the-fly, the only way you have is to change CL_TREX_JSON_DESERIALIZER
class like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With