Question

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.

Was it helpful?

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.

OTHER TIPS

XE3 has "built in" JSON support (see docwiki), but I have heard (haven't used it myself) that it isn't very well optimised. So perhaps look for some thirdparty option like SuperObject.

Your task is easily achievable using TSvSerializer which is included in my delphi-oop library. You only need to declare your model type and deserialize it from your json string. Your model (very simplified incomplete and untested version) should look something like this:

type
  TStats = class
  public
    property health: Integer read fhealth write Fhealth;
    ... 
  end;

  TTalent = class
  public
    property tier: Integer read Ftier write Ftier;
    ...
  end;

  TMainTalent = class
  public
    property selected: Boolean read Fselected write Fselected;
    property talents: TObjectList<TTalent> read Ftalents write Ftalents;
  end;

  TWowCharacter = class
  public
    property lastModified: Int64 read FlastModified write FlastModified;
    property name: string read Fname write Fname;
    ...
    property stats: TStats read Fstats write Fstats;
    property talents: TObjectList<TMainTalent> read Ftalents write Ftalents;
    ...
  end;

Then you just need to do:

uses
  SvSerializer;

var
  LWowCharacter: TWowCharacter;
begin
  LWowCharacter := TWowCharacter.FromJson(YourJsonString);
...

You can find my contact email in delphi-oop project, ask me if something's unclear, I'll try to help you in my spare time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top