views:

155

answers:

2

I'm writing a little web service which generates SO/SF/SU/MSO user flair in the form of an image with various "themes". I find this preferable to using the HTML/JS solutions offered by SO as it's more flexible and also works better in forum signatures.

I'm retrieving the data using the apparently unofficial API (More info here). I can have the data in HTML or JSON. I assumed the JSON would be easier to parse.

Unfortunately, I'm not great at regexes. and the best I can come up with is some very hacky sub-stringing. I believe a regex should be the most elegant solution but would welcome other suggestions.

Can someone please point me in the right direction for a regex that matches ID, GravatarURL, ProfileURL, DisplayName, Reputation and Badge Counts (Bronze/Silver/Gold).

FWIW This is to be used in a VB.Net project (in case that affects the syntax at all)

{"id":1,"gravatarHtml":"\u003cimg src=\"http://www.gravatar.com/avatar/51d623f33f8b83095db84ff35e15dbe8?s=50&d=identicon&r=PG\" height=\"50\" width=\"50\" alt=\"\"\u003e","profileUrl":"http://stackoverflow.com/users/1/jeff-atwood","displayName":"Jeff Atwood","reputation":"18,446","badgeHtml":"\u003cspan title=\"8 gold badges\"\u003e\u003cspan class=\"badge1\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e8\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"57 silver badges\"\u003e\u003cspan class=\"badge2\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e57\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"72 bronze badges\"\u003e\u003cspan class=\"badge3\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e72\u003c/span\u003e\u003c/span\u003e"}

or a slightly more readable format:

{
"id":1,
"gravatarHtml":"\u003cimg src=\"http://www.gravatar.com/avatar/51d623f33f8b83095db84ff35e15dbe8?s=50&d=identicon&r=PG\" height=\"50\" width=\"50\" alt=\"\"\u003e",
"profileUrl":"http://stackoverflow.com/users/1/jeff-atwood",
"displayName":"Jeff Atwood",
"reputation":"18,446",
"badgeHtml":"
    \u003cspan title=\"8 gold badges\"\u003e\u003cspan class=\"badge1\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e8\u003c/span\u003e\u003c/span\u003e
    \u003cspan title=\"57 silver badges\"\u003e\u003cspan class=\"badge2\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e57\u003c/span\u003e\u003c/span\u003e
    \u003cspan title=\"72 bronze badges\"\u003e\u003cspan class=\"badge3\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e72\u003c/span\u003e\u003c/span\u003e
"
}

(NB: It's worth noting that if you don't have any of a certain badge, there's no entry for that badge at all rather than showing a "0")

Unfortunately, I don't even know where to start with the regex so any help, suggestions or documentation greatly appreciated

[Edit]

In case any of you are interested, some screenshots of the flair as a work in progress are available here: Me, Jeff Atwood, Joel Spolsky

I'll make it publicly accessible if anyone wants their own?

[/Edit]

+5  A: 

First of all, don't use regex to parse JSON. Like HTML, JSON is not a regular language, and thus regexes won't be able to truly parse it - only provide a poor approximation, which can easily lead to headaches down the road.

Instead, you probably want to look into JSON parsers like JavaScriptSerializer or Json.NET.

Amber
Thanks - That's a much better than a regex and useful to know
Basiclife
+3  A: 

Have you considered using stackexchange-api

Here is the code sample for what you are trying to achive

        User JeffAtwood =
          User.GetUserWithId(123456, StackExchangeSite.StackOverflow);

        long ID = JeffAtwood.Id;
        Uri gravatarHtml = JeffAtwood.Gravatar;
        String name = JeffAtwood.Name;           
        List<ReputationChange> repGraph = 
            JeffAtwood.ReputationGraph[DateTime.Now.AddDays(-3), DateTime.Now].ToList();
        long totalReputations = JeffAtwood.Reputation;
        List<Badge> badges = JeffAtwood.Badges.ToList();
         .
         .

There are hundreds of other bits you can get using simple built-in methods, other than Requesting and then parsing through Json response.

And for parsing the Jason Response, best library to use is

Asad Butt
As well as answering my question, you solved the problem behind it. Many Thanks. I had no idea that API existed - definitely going to be a boon.
Basiclife
hope to see a stunning Add-in for SOF soon!!
Asad Butt