使用 JSON 或 Hash 时将 NULL 替换为 NIL

问题描述 投票:0回答:1

我有以下 JSON,我正在尝试将

null
替换为
nil
,因为 ruby 无法编译它。

我收到以下错误

undefined local variable or method "null" for main:Object (NameError)

警报描述和单位为

null

我该如何解决这个问题?

jsondata = {
  AlarmName: "Zabbix PY 5XX - By Stage QA",
  AlarmDescription: null,
  AWSAccountId: "123456",
  NewStateValue: "ALARM",
  NewStateReason:
    "Threshold Crossed: 1 out of the last 1 datapoints [1.0 (06/11/21 12:16:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
  StateChangeTime: "2021-11-06T12:17:43.811+0000",
  Region: "Asia Pacific (Mumbai)",
  AlarmArn: "arn:aws:cloudwatch:ap-south-1:123456:alarm:Zabbix PY 5XX - By Stage QA",
  OldStateValue: "INSUFFICIENT_DATA",
  Trigger: {
    MetricName: "5XXError",
    Namespace: "AWS/ApiGateway",
    StatisticType: "Statistic",
    Statistic: "MINIMUM",
    Unit: null,
    Dimensions: [
      { value: "zabbixPy-API", name: "ApiName" },
      { value: "qa", name: "Stage" },
    ],
    Period: 60,
    EvaluationPeriods: 1,
    ComparisonOperator: "GreaterThanOrEqualToThreshold",
    Threshold: 1,
    TreatMissingData: "- TreatMissingData: missing",
    EvaluateLowSampleCountPercentile: "",
  },
}

puts jsondata

错误:

Output:

HelloWorld.rb:6:in `<main>': undefined local variable or method `null' for main:Object (NameError)

  AlarmDescription: null,
json ruby-on-rails ruby
1个回答
0
投票

又是我,我一直在尝试一些事情。告诉我这是否能解决您的问题。

require 'json'

texte = %q(
{
  AlarmName: "Zabbix PY 5XX - By Stage QA",
  AlarmDescription: null,
  AWSAccountId: "123456",
  NewStateValue: "ALARM",
  NewStateReason:
    "Threshold Crossed: 1 out of the last 1 datapoints [1.0 (06/11/21 12:16:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
  StateChangeTime: "2021-11-06T12:17:43.811+0000",
  Region: "Asia Pacific (Mumbai)",
  AlarmArn: "arn:aws:cloudwatch:ap-south-1:123456:alarm:Zabbix PY 5XX - By Stage QA",
  OldStateValue: "INSUFFICIENT_DATA",
  Trigger: {
    MetricName: "5XXError",
    Namespace: "AWS/ApiGateway",
    StatisticType: "Statistic",
    Statistic: "MINIMUM",
    Unit: null,
    Dimensions: [
      { value: "zabbixPy-API", name: "ApiName" },
      { value: "qa", name: "Stage" }
    ],
    Period: 60,
    EvaluationPeriods: 1,
    ComparisonOperator: "GreaterThanOrEqualToThreshold",
    Threshold: 1,
    TreatMissingData: "- TreatMissingData: missing",
    EvaluateLowSampleCountPercentile: ""
  }
}
)

texte = texte.gsub('null', 'nil')
ruby_hash = eval(texte)

puts ruby_hash.inspect
puts "----"
puts ruby_hash[:Trigger][:EvaluationPeriods]
© www.soinside.com 2019 - 2024. All rights reserved.