如何使用 Json.NET 反序列化魔兽世界拍卖 API 中的 json 数据

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

这是 json 数据的示例片段 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}]}
}

(虽然根据真实数据显然有数千次拍卖。)

我想对此进行反序列化,忽略领域数据,只是将拍卖放入一个漂亮干净的 List<WowAuction> 对象中,WowAuction 为:

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

我无法思考如何做到这一点,json API returns 对我来说似乎相当混乱(尽管我承认我没有工作过json 之前)。

据我所知,有一个名为 "auctions" 的集合,其中有一个字段也称为 "auctions",它是一个 table,table 然后包含拍卖数据行。我将如何反序列化它?

有很多方法可以做到这一点,但最简单的方法是创建一个与 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);

扩展@slvnperron 的回答。

首先,构建您的 类。我建议使用像 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; }
    }

其次,解析你的json。我建议使用像 Json.net 这样的工具。你可以用nuget安装它。

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

这里我们的输出将是:

Molten Core

dotnetfiddle 上的工作示例。

以这种方式建立域模型并反序列化数据。

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; }
}

稍后您可以使用以下语句反序列化您的数据

JsonConvert.DeserializeObject<WowAuction>(data); 

自从最初提出这个问题以来,情况发生了一些变化。 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 已经定义了模型 类 并为您处理反序列化。

ArgentPonyWarcraftClient NuGet 包的示例如下。它显示每次拍卖可用信息的子集。

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}");
    }
}