Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用json.NET从魔兽世界拍卖API反序列化json数据_C#_Json_Json.net_Deserialization_World Of Warcraft - Fatal编程技术网

C# 如何使用json.NET从魔兽世界拍卖API反序列化json数据

C# 如何使用json.NET从魔兽世界拍卖API反序列化json数据,c#,json,json.net,deserialization,world-of-warcraft,C#,Json,Json.net,Deserialization,World Of Warcraft,以下是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","

以下是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}]}
}
(虽然很明显,有真实的数据,有数千次拍卖。)

我希望将其反序列化,忽略领域数据,将拍卖放入一个干净的
列表
对象中,其中
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)


据我所知,有一个叫做“拍卖”的集合,里面有一个叫做“拍卖”的字段,它是一个表,该表包含拍卖数据行。我该如何反序列化它?

有很多方法可以做到这一点,但最简单的方法是创建一个与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);
公共类WoWAuctionResponse{
公共信息领域{get;set;}
公共WOWAuctionBody拍卖{get;set;}
}
公共类WoWAuctionsBody{
公开列表拍卖{get;set;}
}
// ...
反序列化对象(json);

有很多方法可以做到这一点,但最简单的方法是创建一个与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);
公共类WoWAuctionResponse{
公共信息领域{get;set;}
公共WOWAuctionBody拍卖{get;set;}
}
公共类WoWAuctionsBody{
公开列表拍卖{get;set;}
}
// ...
反序列化对象(json);

有很多方法可以做到这一点,但最简单的方法是创建一个与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);
公共类WoWAuctionResponse{
公共信息领域{get;set;}
公共WOWAuctionBody拍卖{get;set;}
}
公共类WoWAuctionsBody{
公开列表拍卖{get;set;}
}
// ...
反序列化对象(json);

有很多方法可以做到这一点,但最简单的方法是创建一个与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);
公共类WoWAuctionResponse{
公共信息领域{get;set;}
公共WOWAuctionBody拍卖{get;set;}
}
公共类WoWAuctionsBody{
公开列表拍卖{get;set;}
}
// ...
反序列化对象(json);

扩展@slvnperron的答案

首先,构建您的类。我建议使用类似的工具


工作示例。

扩展@slvnperron的答案

首先,构建您的类。我建议使用类似的工具


工作示例。

扩展@slvnperron的答案

首先,构建您的类。我建议使用类似的工具


工作示例。

扩展@slvnperron的答案

首先,构建您的类。我建议使用类似的工具


工作示例。

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

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); 
JsonConvert.DeserializeObject(数据);

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

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); 
JsonConvert.DeserializeObject(数据);

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

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); 
JsonConvert.DeserializeObject(数据);

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

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); 
JsonConvert.DeserializeObject(数据);

自从最初提出这个问题以来,情况发生了一些变化。现在包括游戏数据和配置文件API。正如这里的其他答案所描述的,您可以创建模型类并使用或类似的库来处理反序列化。还有一些NuGet包,如或,已经定义了模型类并为您处理反序列化

下面是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}");
    }
}
string clientId=“CLIENT-ID-GOES-HERE”;
字符串clientSecret=“CLIENT-SECRET-GOES-HERE”;
int connectedRealmId=1146;
IAuctionHouseApi warcraftClient=新的warcraftClient(
clientId:clientId,
clientSecret:clientSecret,
地区:region.US,
地点:locale.en_US);
RequestResult=wait warcraftClient.GetAuctionsAsync(connectedRealmId,“动态美国”);
如果(结果、成功)
{
AuctionsIndex auctions=结果值;
foreach(拍卖中的拍卖。拍卖)
{
WriteLine($“{auction.Id}:Item Id:{auction.Item.Id}数量:{auction.Quantity}”);
}
}

自从最初提出这个问题以来,情况发生了一些变化。现在包括游戏数据和配置文件API。正如这里的其他答案所描述的,您可以创建模型类并使用或类似的库来处理反序列化。还有一些NuGet包,如或,已经定义了模型类并为您处理反序列化

下面是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}");
    }
}
string clientId=“CLIENT-ID-GOES-HERE”;
字符串clientSecret=“CLIENT-SECRET-GOES-HERE”;
int connectedRealmId=1146;
IAuctionHouseApi warcraftClient=新的warcraftClient(
clientId:clientId,
clientSecret:clientSecret,
地区:region.US,
地点:locale.en_US);
RequestResult=wait warcraftClient.GetAuctionsAsync(connectedRealmId,“动态美国”);
如果(结果、成功)
{
AuctionsIndex auctions=结果值;
foreach(拍卖中的拍卖。拍卖)
{
WriteLine($“{auction.Id}:Item Id:{auction.Item.Id}数量:{auction.Quantity}”);
}
}

自从最初提出这个问题以来,情况发生了一些变化。现在包括游戏数据和配置文件API。正如这里的其他答案所描述的,您可以