Four Ways To Parse JSON Into Objects With Delphi XE6 Firemonkey On Android And IOS

Delphi XE6 Firemonkey Parse JStephen Bell from Embarcadero recently posted a blog post on how to parse JSON with a single line of code in Delphi XE6 Firemonkey. I had to parse some JSON recently myself and I wanted to try out some different ways of doing that as I usually use XSuperObject. I came up with four different ways to parse out a piece of JSON. Some of the ways are completely static and one of them is completely dynamic. The four ways I tried are the TJson.JsonToObject way into a custom object, using the straight XSuperObject way into a custom object, using XSuperObject into a generic Tuple, and using XSuperObject into a dynamic TStringList. When I parse the JSON I put the object that I create to store the data in into a TStringList for usage later. Full source code for a sample project demonstrating each way is available at the bottom. All four ways will probably also work in Delphi XE5 and AppMethod in addition to cross platform on Windows, OSX, Android, and IOS.

Here is the straight XSuperObject into a custom object. This way is pretty static and requires that you define an object, TPizza, in the type section:

Here is the TJson.JsonToObject into a custom object. This way takes less code than the direct XSuperObject way but still requires that you define an object, TPizza, in the type section:

And here is reading both of the above back out from the TPizza object:

Here is the XSuperObject into a Tuple. For me this code is harder to read but does not require that you define the extra TPizza object type at the top of the unit:

And finally here is the XSuperObject into a TStringList. For me this seems like the most dynamic way. There are no predefined names hard coded into your code and it supports an unlimited number of name value pairs. However, it does only support string types and probably takes up some more memory than some of the other ways:

Download the full source code for the four different ways to parse JSON with Delphi XE6 Firemonkey.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Four Ways To Parse JSON Into Objects With Delphi XE6 Firemonkey On Android And IOS by Delphi XE5 XE6 XE7 XE8 10 Seattle Berlin Firemonkey, Delphi Android, Delphi IOS is licensed under a Creative Commons Attribution 4.0 International License.

One Response to Four Ways To Parse JSON Into Objects With Delphi XE6 Firemonkey On Android And IOS

  1. Pingback: Automatically JSON Serialize A DataSet For REST In Delphi XE6 Firemonkey On Android And IOS | Delphi XE5 XE6 Firemonkey, Delphi Android, Delphi IOS

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
Delphi XE6 Firemonkey Generics Tuples
Add Generic Tuple Support With Generics.Tuples In Firemonkey On Android And IOS

Malcolm Groves from Embarcadero has a new unit posted on Github called Generics.Tuples and it implements generic tuple support in...

Close