2

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.

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
Teun Pronk
  • 1,367
  • 12
  • 24
  • 1
    See [Delphi JSON library for XE2 available for object serialization](http://stackoverflow.com/q/7731833/576719) for different Delphi libraries for parsing `json`. – LU RD Dec 17 '12 at 10:27
  • 2
    I think you might give a try to the [`TJSONObject`](http://docwiki.embarcadero.com/Libraries/XE3/en/Data.DBXJSON.TJSONObject) or libraries such as [`SuperObject`](http://www.progdigy.com/?page_id=6). – TLama Dec 17 '12 at 10:28
  • Thanks, will look into both of these – Teun Pronk Dec 17 '12 at 10:28
  • I feel like a complete noob haha, I found the `SuperObject` earlier and looked into the demos, tried to work with it but couldnt really figure it out. I'm new to delphi, only started working with it since 01-10-2012 so. I contacted the developer of `SuperObject` still awaiting an answer. :) – Teun Pronk Dec 17 '12 at 10:32
  • 1
    @Teun forget it. Henri is usually very busy with his most current project and abandons finished ones. Also if he wished to explain basics in more details, he would not discuss it in private mail but would create and publish more detailed documentation for everyone. So you'd probably better go google and search "how to do this and that using SuperObject" rather than thinking Henrui would explain that to your personally. You can also try soem other lib, like Synopse, whose forum is usually more active than Henri's. – Arioch 'The Dec 17 '12 at 11:01
  • @Arioch'The Okay, thanks. Been googling a bit after seeing the answer posted by ain. Looks like I can do its with the functions provided by delphi see [link](http://stackoverflow.com/questions/10808912/how-to-parse-nested-json-object-in-delphi-xe2) but thanks again. – Teun Pronk Dec 17 '12 at 11:04
  • @Teun laso please read: http://www.catb.org/esr/faqs/smart-questions.html If you want answers, the questions should better be narrow and concrete. In a pattern "i want to do this. I tried the library that. I made my code this. And it behaves wrong in this and that. help me fix my code" The more vague question you post - the less chance any one would give you anything but most vague answers – Arioch 'The Dec 17 '12 at 11:05
  • Actually your Havek seems very basic JSON. Why cannot you read it ? while SO help is very scarse - i cannot find what you lost there ? "Parsing a JSON data structure" section shows you how to load your json string/file int othe library. Then "How to read a property value of an object ?" shows you hwo to read the properties like "name" and such. D:/DelphiProjects/Libs/SuperObject/readme.html If you lost at some certain action - then ask better about that action. – Arioch 'The Dec 17 '12 at 11:10
  • 1
    If you have Enterprise-level Delphi - then it's probably true. But i think DB-Express is too heavy-weight platform to have as not needed dependency and to use for fast parsing. But it is your choice of course. – Arioch 'The Dec 17 '12 at 11:13
  • @Arioch'The You solved my entire issue here, it was just one of those RTFM kind of things... If you could put that as an answer to this question I can accept it since this actually is the solution to the problem. Thanks again :) – Teun Pronk Dec 17 '12 at 13:08
  • @Teun u mean references to SuperObject help file ? i hardly did anythign but that. – Arioch 'The Dec 17 '12 at 15:03
  • 1
    "I'm new to Delphi, only started working with it since 01-10-2012 so" 1) could i ask to refrain from that date format on international sites? thanks to USA, it is kinda ambiguous if you meant 01-10-2012 or 10-01-2012 // 2) did you never worked wit hDelphi before ? if so, then why did you started it ? it is a common point that delphi niche is shrinkind and no "new blood" enters, so it is interesting to learn of counter-examples. – Arioch 'The Dec 20 '12 at 07:32
  • I started working with deplhi around the 1st of oct. Before that I never worked with Delphi ever. I started working with it for my job. I worked with VB+ASP.NET for a year but my boss didnt give me a new contract. I needed a new job and found one with a company that works with Delphi. So thats the reason I started :) – Teun Pronk Dec 20 '12 at 11:40
  • 1
    PS. After Embarcadero pirated his GDI+ wrapper, Henry claimed he would never more do something for Delphi again. Pity... – Arioch 'The Jan 10 '13 at 11:38

3 Answers3

5

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.

Arioch 'The
  • 15,799
  • 35
  • 62
  • Thanks for this, I know its a small JSON now. But look at what you can add, and most of this I will add [link](http://blizzard.github.com/api-wow-docs/#character-profile-api) It can become quite big ;) – Teun Pronk Dec 18 '12 at 13:35
  • You can't go wrong if use Synapse's components and libraries. Their support and dedication is out of this world. – Leonardo Herrera Jun 23 '15 at 20:47
  • @LeonardoHerrera synOpse not synApse - two totally different houses and libs – Arioch 'The Jun 24 '15 at 10:05
  • @Arioch'The - yup, you are totally right, that was a typo. I was referring to Arnaud's project thought (mormot / Synopse.) – Leonardo Herrera Jun 24 '15 at 13:05
4

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.

ain
  • 22,394
  • 3
  • 54
  • 74
3

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.

Linas
  • 5,485
  • 1
  • 25
  • 35