c# - 循环遍历 JObject c#

标签 c# json json.net

我正在使用 JSON.NET 来解析我的 JSON:

JObject jsonObject = JObject.Parse(myJson);

我的 JSON 看起来像这样:

{
  "_links": {
    "self": {
      "href": "https://api-uat.dwolla.com/accounts/069b759d-6267-42d6-b30b-5d03ddd25673/funding-sources",
      "type": "application/vnd.dwolla.v1.hal+json",
      "resource-type": "funding-source"
    }
  },
  "_embedded": {
    "funding-sources": [
      {
        "_links": {
          "self": {
            "href": "https://api-uat.dwolla.com/funding-sources/0b2f6a31-b909-4c7d-a9f8-6253e5f791d0",
            "type": "application/vnd.dwolla.v1.hal+json",
            "resource-type": "funding-source"
          },
          "account": {
            "href": "https://api-uat.dwolla.com/accounts/069b759d-6267-42d6-b30b-5d03ddd25673",
            "type": "application/vnd.dwolla.v1.hal+json",
            "resource-type": "account"
          },
          "balance": {
            "href": "https://api-uat.dwolla.com/funding-sources/0b2f6a31-b909-4c7d-a9f8-6253e5f791d0/balance",
            "type": "application/vnd.dwolla.v1.hal+json",
            "resource-type": "balance"
          }
        },
        "id": "0b2f6a31-b909-4c7d-a9f8-6253e5f791d0",
        "status": "verified",
        "type": "bank",
        "name": "Bank Of America",
        "created": "2017-03-22T12:54:51.000Z",
        "removed": false,
        "channels": [
          "ach"
        ],
        "bankName": "SANDBOX TEST BANK"
      },
      {
        "_links": {
          "self": {
            "href": "https://api-uat.dwolla.com/funding-sources/2235c3f5-03d6-4b7c-8ffb-c389c94375eb",
            "type": "application/vnd.dwolla.v1.hal+json",
            "resource-type": "funding-source"
          },
          "account": {
            "href": "https://api-uat.dwolla.com/accounts/069b759d-6267-42d6-b30b-5d03ddd25673",
            "type": "application/vnd.dwolla.v1.hal+json",
            "resource-type": "account"
          }
        },
        "id": "2235c3f5-03d6-4b7c-8ffb-c389c94375eb",
        "status": "verified",
        "type": "bank",
        "name": "Superhero Savings Bank",
        "created": "2017-03-17T06:39:28.000Z",
        "removed": true,
        "channels": [
          "ach"
        ],
        "bankName": "SANDBOX TEST BANK"
      }      
    ]
  }
}

在这里我可以有 N 个资金来源。我想获取 selfhref,其中删除的每个资金来源都是 false。所以在上面的例子中我需要:

href:"https://api-uat.dwolla.com/funding-sources/0b2f6a31-b909-4c7d-a9f8-6253e5f791d0"

我试图通过遍历 JObject 来实现这一点:

foreach (var x in jsonObject)
{
    if(x.Key == "_embedded")
    {
        foreach (var fundingSources in x.Value["funding-sources"])
        {
            foreach (var y in fundingSources)
            {

            }
        }
    }
}

不幸的是,我无法将资金来源放入数组中。

最佳答案

像这样尝试:

JObject jsonObject = JObject.Parse(myJson);
foreach (JToken fundingSource in jsonObject.SelectToken("_embedded.funding-sources"))
{
    bool removed = (bool)fundingSource["removed"];
    if (!removed)
    {
        string href = (string)fundingSource.SelectToken("_links.self.href");
        Console.WriteLine(href);
    }
}

fiddle :https://dotnetfiddle.net/Jhw8w6

或者,您可以像这样使用 LINQ:

JObject jsonObject = JObject.Parse(myJson);
List<string> links = jsonObject.SelectToken("_embedded.funding-sources")
    .Where(fundingSource => !(bool)fundingSource["removed"])
    .Select(fundingSource => (string)fundingSource.SelectToken("_links.self.href"))
    .ToList();

Console.WriteLine(string.Join(Environment.NewLine, links));

fiddle :https://dotnetfiddle.net/gnyGgk

关于c# - 循环遍历 JObject c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43051014/

相关文章:

c# - 使用 JSON.NET 和 NodaTime 从 JSON 反序列化 LocalTime 导致 NodaTime.Text.UnparsableValueException

c# - 使用 JsonPath C# 获取/设置对象

c# - 使用公开加密/解密值的单独类通过 Entity Framework 加密解密数据 : Linq statements fails

c# - 替换方法的主体会删除以下换行符

c# - 从网页获取数据

python:我需要附加一个带有 json 的文件,但它不输出有效的 json

ios - 调试 Alamofire 请求

c# - 启动 Windows 服务时出现错误 1053(在 Visual Studio 中创建的 exe)

javascript - 生成动态画廊jquery

c# - 在 C# 中导航 JSON 数据