Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize an anonymous JSON array?

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
like image 780
inetphantom Avatar asked May 31 '16 08:05

inetphantom


People also ask

How to deserialize from a JSON object?

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: . . .

How do I deserialize an array with x4 y4 z4?

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.

Why can’t I use jsonelement dynamic with the native deserializer?

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.

How to put an identifier in a JSON array string?

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);


1 Answers

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.

like image 107
Suncatcher Avatar answered Oct 22 '22 01:10

Suncatcher