【问题标题】:Parsing JSON with Powershell from dev.battle.net (World of Warcraft API)使用来自 dev.battle.net 的 Powershell 解析 JSON(魔兽世界 API)
【发布时间】:2016-09-06 22:48:21
【问题描述】:

因为我今天不能上班(阑尾切除术后),但仍想提高我的 PowerShell 技能,我决定尝试访问魔兽世界 API 并下载拍卖行数据以获得乐趣。

$apiKey = "myapikey"
$serverName = "MyServer"
$webAddress = "https://us.api.battle.net/wow/auction/data/"

$url = $webAddress + $serverName + "?apikey=" + $apiKey

$response = Invoke-RestMethod -Uri $url -ContentType "application/json"

$aucURL = $response.files.url
$aucTime = $response.files.lastModified

$response = Invoke-RestMethod -Uri $aucURL

$aucData = $response.auctions #Always returns blank

到了这一步,我得到了一个巨大的数据列表,如下所示,在我的一生中,我似乎无法访问拍卖数据。

{
"realms": [
    {"name":"MyServer","slug":"MyServer"}],
"auctions": [
    {"auc":723774847,"item":109167,"owner":"Laddypally","bid":369550,"buyout":389000,"quantity":1,"timeLeft":"VERY_LONG","rand":0,"seed":0,"context":1},
    {"auc":724234542,"item":2996,"owner":"Mazramtaim","bid":760000,"buyout":800000,"quantity":20,"timeLeft":"VERY_LONG","rand":0,"seed":0,"context":0},
    {"auc":723207271,"item":133563,"owner":"Alsalak","bid":1187500,"buyout":1250000,"quantity":5,"timeLeft":"LONG","rand":0,"seed":0,"context":0}]}

我使用 $responses.whatever 的方法在过去与我的工作相关的 PS 东西都有效,但在这里却不行。有什么原因吗?

【问题讨论】:

  • Invoke-RestMethod 应该将 JSON 响应转换为对象,如果我在您的示例内容上这样做,那么 $x.auctions 工作正常。所以我猜服务器没有发回任何东西(例如内容类型标头)来告诉 PS 将其从 JSON 转换,并且它正在将其视为纯文本响应。尝试使用 $response = $response | ConvertFrom-JSON 然后 $response.auctions 自己转换它
  • 我尝试从 JSON 转换,但因为被拉出的文件是 ~10 Mb,所以它令人窒息,并给我一个关于最大文件大小的错误。我正在单独调查这个错误,但到目前为止我发现的答案似乎很混乱。我认为你在做某事。第一个 $response 作为 PSCustomObject 返回,第二个作为字符串返回。文件本身只是命名为“auction-api-us.worldofwarcraft.com/auction-data/…
  • 我认为这里描述的一种方法可以解决大小问题:stackoverflow.com/questions/16854057/…
  • 我无法在 PowerShell v5 中复制此问题。也许您应该更新您的 PowerShell 版本?我刚刚提取了萨格拉斯 (19.68MB) 的拍卖数据,并且能够使用 $AucData 访问它,所有 112606 个条目。
  • @TheMadTechnician,我会试一试 PS 5 并告诉你

标签: api powershell world-of-warcraft


【解决方案1】:

升级到 PowerShell 5 解决了这个问题。我没有更改任何代码,所以问题一定出在 PS4 如何处理大型 JSON 请求。

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多