Search code examples
jsondelphiapidelphi-xe3world-of-warcraft

Finding it difficult to process JSON in delphi


I'm currently working on an application that will get data of your character from the WoW armory. Example character: My WoW Character(link)

I will get all the info I want by calling the API provided by Blizzard and I will get the response in JSON. Example JSON: JSON response for the character above(link)

At first I tried get the data from the JSON by string manipulation. This mean, splitting my strings, searching for keywords in the string to find the position and formatting that into individual pieces of data such as talents and stats.

This worked great at the beginning but as I wanted more data this became harder because of the many functions I ran on all the strings it just became one big blur and unclear to see what I was doing at that moment.

Is there a good way to process my JSON? I was thinking about getting the JSON and creating an empty class. While working through the JSON it would generate properties and store the values in there. But I have no idea if and how its possible to generate properties dynamically.

In the future I would like to get even more data but first I want to get this up and running before even thinking about that.

Does anyone have any ideas/advice on this?

Thanks in advance.


Solution

  • Your JSON seems rather short and basic. It does not seem you need special speed or exotic features. http://jsonviewer.stack.hu/#http://eu.battle.net/api/wow/character/moonglade/Xaveak?fields=stats,talents

    And while since Delphi XE2 you really have stock JSON parser as part of DB-Express suite, still there are concerns:
    1. It was told to cause problems with both speed and reliability.
    2. It would make you program dependent on DB-Express package (why, if you not actually using it for DB access?) 3. It would bind your future to Enterprise edition of Delphi.

    So you'd better try some 3rd-party library.

    One of the fastest would probably be Synopse JSON parser, side-project of their mORMot library. It is generally good code, with large attention to speed and developers actively helping on their forum.

    One more known and used library would be Henri Gourvest's SuperObject. It made claims to be the fastest parser for Delphi, and while due to above that is probably no more true, the speed is quite adequate for most tasks. Henri himself is not actively supporting his ex-projects, always doing something new, so the scarce documentation (also duplicated in install package) would be all you have officially, plus there is a forum where other users might help you. OTOH the main idea behind SuperObject design was uniformity, and while some tasks could really be documented better - that is mostly due to uncertainty "if this task would really work in uniform matter without any special treatment". But usually it does.
    PS. Since that is wiki you may try to enhance it for future users ;-)

    So coming back to documentation, you would need

    1) to load the whole JSON to the library. That you can do via creating TStream by your http library or providing string buffer wth the data: that is Parsing a JSON data structure section of the manual

    2) reading values like "name" and "level" - described in How to read a property value of an object ? section there.

    3) enlist arrays like "talents" - described in Browsing data structure section.