从currencycloud.com获取响应时,从jtoken获取包含数组的索引元素

时间:2015-07-22 07:41:30

标签: json.net

我有以下JSON:

https://connect.currencycloud.com/documentation/api-docs/post-beneficiaries-create

{
  "details": [
    {
      "payment_type": "regular",
      "iban": "^[0-9A-Z]{1,34}$",
      "bic_swift": "^[0-9A-Z]{8}$|^[0-9A-Z]{11}$",
      "beneficiary_entity_type": "individual"
    },
    {
      "payment_type": "regular",
      "iban": "^[0-9A-Z]{1,34}$",
      "bic_swift": "^[0-9A-Z]{8}$|^[0-9A-Z]{11}$",
      "beneficiary_entity_type": "company"
    },
    {
      "payment_type": "priority",
      "beneficiary_entity_type": "individual",
      "beneficiary_address": "^.{1,255}",
      "beneficiary_city": "^.{1,255}",
      "beneficiary_country": "^[A-z]{2}$",
      "beneficiary_first_name": "^.{1,255}",
      "beneficiary_last_name": "^.{1,255}",
      "iban": "^[0-9A-Z]{1,34}$",
      "bic_swift": "^[0-9A-Z]{8}$|^[0-9A-Z]{11}$"
    },
    {
      "payment_type": "priority",
      "beneficiary_entity_type": "company",
      "beneficiary_address": "^.{1,255}",
      "beneficiary_city": "^.{1,255}",
      "beneficiary_country": "^[A-z]{2}$",
      "beneficiary_company_name": "^.{1,255}",
      "iban": "^[0-9A-Z]{1,34}$",
      "bic_swift": "^[0-9A-Z]{8}$|^[0-9A-Z]{11}$"
    }
  ]
}

我想从这个数组中检索第三个列表..那是

    {
      "payment_type": "priority",
      "beneficiary_entity_type": "individual",
      "beneficiary_address": "^.{1,255}",
      "beneficiary_city": "^.{1,255}",
      "beneficiary_country": "^[A-z]{2}$",
      "beneficiary_first_name": "^.{1,255}",
      "beneficiary_last_name": "^.{1,255}",
      "iban": "^[0-9A-Z]{1,34}$",
      "bic_swift": "^[0-9A-Z]{8}$|^[0-9A-Z]{11}$"
    },
有人请帮帮我

1 个答案:

答案 0 :(得分:0)

您可以使用JToken.Load()将JSON加载到内存中,然后使用SelectToken查询JSON对象层次结构中的对象:

        var root = JToken.Parse(json);
        var item = root.SelectToken("details[2]");

数组索引从零开始。

<强>更新

选择对象内的特定属性,例如"payment_type",将属性的名称添加到现有查询,并强制转换为预期类型:

        var payment_type = (string)root.SelectToken("details[2].payment_type");

SelectToken支持此处指定的JSONPath查询语法:JSONPath - XPath for JSON