【问题标题】:Get specific Json field with NewtonSoft使用 NewtonSoft 获取特定的 Json 字段
【发布时间】:2020-10-12 21:19:00
【问题描述】:

我正在使用 newtonsoft,我需要在 Json 响应中获取两个特定字段:

{
    "id": "1233323",
    "quoteOptions": [{
        "copay": "20%",
        "deductables": [{
            "deductable": "$250",
            "quoteOptions": [{
                "pricingAndLimits": {
                    "reimbursement": "80%",
                    "copay": "20%",
                    "deductable": "$250",
                    "limit": "$4,000",
                    "annualPremium": "$413.00",
                    "monthlyPremium": "$34.00"
                }
            }]
        }]
    }]
}

我需要在旧版 Intranet (VB.NET) 上执行此操作。我特别需要获取monthlyPremium 字段和id 字段。

我试过了

Dim json As String = Result
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
Dim output As String = ""
Dim Premium As String = ser("quoteOptions")("deductables")("quoteOptions")("pricingAndLimits")("monthlyPremium")

但我在“免赔额”处收到错误(不存在)。

谢谢:)

【问题讨论】:

  • 请在您的示例中包含一个实际的 JSON 字符串,而不是您认为的解释。
  • 嗨 Drew,Json 字符串就在帖子的开头
  • 使用无效键值访问的 JArray 值:“deductables”。预期为 Int32 数组索引。这就是我得到的错误
  • 当你看到[]时,表示你必须输入[number]。所以,ser("quoteOptions")[0]...
  • 我用 ("deductables")[0], ("deductables"[0]) 试过了,但还是不行。

标签: json vb.net json.net


【解决方案1】:

我让它像这样工作:

Money = ser("quoteOptions")(0)("deductables")(0)("quoteOptions")(0)("pricingAndLimits")("monthlyPremium")

感谢克鲁德

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多