简体   繁体   English

通过JObject c#循环

[英]Looping through JObject c#

I am using JSON.NET to parse my JSON: 我正在使用JSON.NET解析我的JSON:

JObject jsonObject = JObject.Parse(myJson);

My JSON looks something like this: 我的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"
      }      
    ]
  }
}

Here I could have N number of funding sources. 在这里,我可以有N个资金来源。 I want to get the href of self where removed is false for each funding sources. 我想获取selfhref ,对于每个资金来源,移除的都是false的。 So in the above example I need: 因此,在上面的示例中,我需要:

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

I tried to achieve this by looping through the JObject : 我试图通过遍历JObject来实现这JObject

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

            }
        }
    }
}

Unfortunately I could not get the funding sources into an array. 不幸的是,我无法将资金来源汇总。

Try like this: 尝试这样:

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 小提琴: https : //dotnetfiddle.net/Jhw8w6

Alternatively you can use LINQ like this: 或者,您可以像这样使用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 小提琴: https : //dotnetfiddle.net/gnyGgk

Try this code block: 试试这个代码块:

JObject jsonObject = JObject.Parse ( myJson );

foreach ( var x in jsonObject )
{
    if ( x.Key == "_embedded" )
    {
        foreach ( var source in x.Value [ "funding-sources" ] )
        {
            var removedAttribute = source [ "removed" ];
            if ( removedAttribute != null && bool.Parse ( removedAttribute.ToString () ) == false )
            {
                var links = source [ "_links" ];
                var self = links [ "self" ];
                var href = self [ "href" ];
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM