【问题标题】:Powershell handle JSON Response of rest API (drives me crazy)Powershell 处理 REST API 的 JSON 响应(让我发疯)
【发布时间】:2021-06-17 08:05:15
【问题描述】:

我正在使用一个 REST API 并获得一个 JSON 作为响应。

{
  "price": 0,
  "price_currency": "EUR",
  "balance": {
    "API Decode": 19
  },
  "decode": [
    {
      "label": "VIN",
      "value": "VSSZZZ7NZGV714632"
    },
    {
      "label": "Make",
      "value": "Seat"
    },
    {
      "label": "Manufacturer",
      "value": "Sociedad Espanola De Automoviles"
    },
    {
      "label": "Plant Country",
      "value": "Spain"
    },
    {
      "label": "Manufacturer Address",
      "value": "De Turismo S.A. (SEAT), Autovia A II  KM 585, 08760 Martorell  Barcelona"
    },
    {
      "label": "Model",
      "value": "Alhambra"
    },
    {
      "label": "Check Digit",
      "value": "Z"
    },
    {
      "label": "Sequential Number",
      "value": "714632"
    },
    {
      "label": "Product Type",
      "value": "Passenger car"
    },
    {
      "label": "Model Year",
      "value": 2016
    },
    {
      "label": "Body",
      "value": "Wagon"
    },
    {
      "label": "Number of Doors",
      "value": 5
    },
    {
      "label": "Number of Axles",
      "value": 2
    },
    {
      "label": "Length (mm)",
      "value": 4854
    },
    {
      "label": "Width (mm)",
      "value": 1904
    },
    {
      "label": "Height (mm)",
      "value": 1698
    },
    {
      "label": "Track Front (mm)",
      "value": 1571
    },
    {
      "label": "Track Rear (mm)",
      "value": 1617
    },
    {
      "label": "Wheelbase (mm)",
      "value": 2920
    },
    {
      "label": "Wheel Size",
      "value": "205/60 R16 96H"
    },
    {
      "label": "Fuel Type - Primary",
      "value": "Diesel"
    },
    {
      "label": "Engine Displacement (ccm)",
      "value": 1968
    },
    {
      "label": "Transmission",
      "value": "Manual/Standard"
    },
    {
      "label": "Number of Gears",
      "value": 6
    },
    {
      "label": "ABS",
      "value": 1
    },
    {
      "label": "Emission Standard",
      "value": "Euro 6"
    },
    {
      "label": "Permitted trailer load without brakes (kg)",
      "value": 750
    },
    {
      "label": "Permitted trailer load with brakes 12% (kg)",
      "value": 2200
    },
    {
      "label": "Permitted towbar download (kg)",
      "value": 100
    },
    {
      "label": "Wheelbase Array (mm)",
      "value": [
        2920
      ]
    },
    {
      "label": "Wheel Size Array",
      "value": [
        "205/60 R16 96H"
      ]
    }
  ]
}

我想通过名称访问标签 VIN、Make 等的值。 我不能使用索引,因为每次返回的 JSON 都不一样。 有没有办法在 powershell 中按名称访问诸如“排放标准”之类的标签并将其用作参数?

【问题讨论】:

  • 类似($json |ConvertFrom-Json).decode.Where({$_.label -eq 'VIN'}).value?
  • 是的..完全一样..谢谢:)
  • 请避免在您的示例中放置真实或敏感数据。

标签: json powershell api rest


【解决方案1】:

正如 cmets 中所暗示的,您可以使用 the ConvertFrom-Json cmdlet 将 JSON 解析为结构化对象:

# Read json from file
$json = Get-Content .\path\to\input.json

# Parse using ConvertFrom-Json
$data = $json |ConvertFrom-Json

# Now we can filter by name:
$VINValue = $data.decode.Where({$_.label -eq 'VIN'}).value

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2019-02-21
    • 2021-04-23
    • 2011-05-30
    • 2012-11-02
    相关资源
    最近更新 更多