简体   繁体   中英

How to deserialise json data from the World of Warcraft auction API using Json.NET

Here is an example snippet of the json data that the API returns:

{
"realm":{"name":"Molten Core","slug":"molten-core"},
"auctions":{"auctions":[
    {"auc":1880591075,"item":109128,"owner":"Leagra","ownerRealm":"Azjol-Nerub","bid":858600,"buyout":900000,"quantity":100,"timeLeft":"VERY_LONG","rand":0,"seed":0,"context":0},
    {"auc":1879726534,"item":43115,"owner":"Nêwt","ownerRealm":"Azjol-Nerub","bid":5120000,"buyout":5120000,"quantity":16,"timeLeft":"VERY_LONG","rand":0,"seed":835268864,"context":0}]}
}

(Though obviously with real data there's thousands of auctions.)

I'm looking to deserialise this, ignoring the realm data and just putting the auctions into a nice clean List<WowAuction> object, with WowAuction being:

public class WowAuction
{
      public long auc { get; set; }
      public long item { get; set; }
      public long bid { get; set; }
      public long buyout { get; set; }
}

I'm having trouble wrapping my head around how I would do this, the json the API returns seems rather messy to me (though admittedly I haven't worked with json before).

As far as I can tell, there's a collection called "auctions", inside of that is a single field also called "auctions" which is a table, that table then contains rows of auction data. How would I deserialise this?

There are many ways you could do that but the simple way is to create a domain object with the same structure as your JSON:

public class WoWAuctionResponse {
    public WoWRealmInfo Realm {get; set;}
    public WoWAuctionsBody Auctions {get; set;}
}

public class WoWAuctionsBody {
   public List<WoWAuction> Auctions {get; set;}
}

// ...

JsonConvert.DeserializeObject<WoWAuctionResponse>(json);

To extend @slvnperron's answer.

First, build your classes. I recommend to use a tool like json2csharp .

    public class Realm
    {
        public string name { get; set; }
        public string slug { get; set; }
    }

    public class Auction
    {
        public int auc { get; set; }
        public int item { get; set; }
        public string owner { get; set; }
        public string ownerRealm { get; set; }
        public int bid { get; set; }
        public int buyout { get; set; }
        public int quantity { get; set; }
        public string timeLeft { get; set; }
        public int rand { get; set; }
        public int seed { get; set; }
        public int context { get; set; }
    }

    public class Auctions
    {
        public List<Auction> auctions { get; set; }
    }

    public class RootObject
    {
        public Realm realm { get; set; }
        public Auctions auctions { get; set; }
    }

Second, parse your json. i recommend to use a tool like Json.net . You can install it with nuget.

    public static void Main()
    {
        string json = @"{here your json}";
        RootObject m = JsonConvert.DeserializeObject<RootObject>(json);
        Console.WriteLine(m.realm.name.Trim());
    }

here our output will be :

Molten Core

Working example on dotnetfiddle .

Have your domain model this way and deserialize your data.

internal class WowAuction
{

    [JsonProperty("realm")]
    public Realm Realm { get; set; }

    [JsonProperty("auctions")]
    public Auctions Auctions { get; set; }
}


internal class Realm
{

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("slug")]
    public string Slug { get; set; }
}

internal class Auctions
{

    [JsonProperty("auctions")]
    public Auction[] Auctions { get; set; }
}

internal class Auction
{

    [JsonProperty("auc")]
    public int Auc { get; set; }

    [JsonProperty("item")]
    public int Item { get; set; }

    [JsonProperty("owner")]
    public string Owner { get; set; }

    [JsonProperty("ownerRealm")]
    public string OwnerRealm { get; set; }

    [JsonProperty("bid")]
    public int Bid { get; set; }

    [JsonProperty("buyout")]
    public int Buyout { get; set; }

    [JsonProperty("quantity")]
    public int Quantity { get; set; }

    [JsonProperty("timeLeft")]
    public string TimeLeft { get; set; }

    [JsonProperty("rand")]
    public int Rand { get; set; }

    [JsonProperty("seed")]
    public int Seed { get; set; }

    [JsonProperty("context")]
    public int Context { get; set; }
}

Later you can have following statement to deserialize your data

JsonConvert.DeserializeObject<WowAuction>(data); 

Things have changed a bit since this question was originally asked. The World of Warcraft APIs now include Game Data and Profile APIs. As the other answers here describe, you can create model classes and use Json.NET or a similar library to handle the deserialization. There are also NuGet packages like the Argent Pony Warcraft Client or the BattleMuffin Blizzard API Client that have already defined model classes and handle the deserialization for you.

An example with the ArgentPonyWarcraftClient NuGet package follows. It displays a subset of the information available for each auction.

string clientId = "CLIENT-ID-GOES-HERE";
string clientSecret = "CLIENT-SECRET-GOES-HERE";

int connectedRealmId = 1146;

IAuctionHouseApi warcraftClient = new WarcraftClient(
    clientId: clientId,
    clientSecret: clientSecret,
    region: Region.US,
    locale: Locale.en_US);

RequestResult<AuctionsIndex> result = await warcraftClient.GetAuctionsAsync(connectedRealmId, "dynamic-us");

if (result.Success)
{
    AuctionsIndex auctions = result.Value;

    foreach(Auction auction in auctions.Auctions)
    {
        Console.WriteLine($"{auction.Id}: Item ID: {auction.Item.Id} Quantity: {auction.Quantity}");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM