0

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?

4

4 回答 4

1

有很多方法可以做到这一点,但最简单的方法是创建一个与 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);
于 2015-01-26T02:27:39.303 回答
1

扩展@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上的工作示例。

于 2015-01-26T02:40:16.923 回答
1

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

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); 
于 2015-01-26T02:43:19.677 回答
0

自从最初提出这个问题以来,情况发生了一些变化。魔兽世界 API现在包括游戏数据和配置文件 API。正如此处的其他答案所描述的,您可以创建模型类并使用Json.NET或类似的库来处理反序列化。还有一些 NuGet 包,如Argent Pony Warcraft ClientBattleMuffin 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}");
    }
}
于 2020-07-25T19:08:10.837 回答