2017-03-27 1 views
0

c를하는 jobject를 통해 반복 : 여기내 JSON을 구문 분석 JSON.NET을 사용하고 #

{ 
    "_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 번호를 가질 수있다 :

JObject jsonObject = JObject.Parse(myJson); 

내 JSON이 같이 보입니다. hrefself을 가져오고 싶습니다. 각 자금 출처마다 제거 된 곳은 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) 
      { 

      } 
     } 
    } 
} 

불행하게도 내가 배열로 자금 소스를 가져올 수 없습니다.

답변

1

다음과 같이하십시오 :

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); 
    } 
} 

바이올린 : 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)); 

바이올린 : https://dotnetfiddle.net/gnyGgk

+0

이것은 매력처럼 작동합니다. :) 감사합니다! –

+0

기꺼이 도와 드리겠습니다. –

1

이 코드 블록을 시도해보십시오

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" ]; 
      } 
     } 
    } 
}