以下是API返回的json数据的示例片段:
{
"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; }
}
我无法理解如何做到这一点,API返回的json对我来说似乎相当混乱(尽管我承认以前我还没有和json合作过)。
据我所知,有一个名为&#34;拍卖&#34;的集合,其中一个字段也称为&#34;拍卖&#34;这是一个表,该表然后包含拍卖数据行。我该怎么反序化呢?
答案 0 :(得分: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);
答案 1 :(得分: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上的工作示例。
答案 2 :(得分: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);
答案 3 :(得分:0)
自从最初提出这个问题以来,事情发生了一些变化。 World of Warcraft APIs现在包括游戏数据和配置文件API。如此处其他答案所述,您可以创建模型类并使用Json.NET或类似的库来处理反序列化。还有Argent Pony Warcraft Client或BattleMuffin Blizzard API Client之类的NuGet程序包,它们已经定义了模型类并为您处理反序列化。
下面是一个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}");
}
}