Navaz Baig
Navaz Baig

Reputation: 73

How to retrieve object value from Dictionary<string,object> in c#

i have following dictionary in string,object format.

Dictionary<string, object> responseList = null;
    responseList = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(response["Content"]);

In above dictionary I have following JSON data.

> {   "_links": {
>     "account": {
>       "href": "https://api-uat.dwolla.com/accounts/ec297a2c-f681-417b-9668-8f3ebcd33060",
>       "type": "application/vnd.dwolla.v1.hal+json",
>       "resource-type": "account"
>     },
>     "events": {
>       "href": "https://api-uat.dwolla.com/events",
>       "type": "application/vnd.dwolla.v1.hal+json",
>       "resource-type": "event"
>     },
>     "webhook-subscriptions": {
>       "href": "https://api-uat.dwolla.com/webhook-subscriptions",
>       "type": "application/vnd.dwolla.v1.hal+json",
>       "resource-type": "webhook-subscription"
>     },
>     "customers": {
>       "href": "https://api-uat.dwolla.com/customers",
>       "type": "application/vnd.dwolla.v1.hal+json",
>       "resource-type": "customer"
>     }   } }

I want to retrieve href of account section which is "https://api-uat.dwolla.com/accounts/ec297a2c-f681-417b-9668-8f3ebcd33060". I have retrieved object part of dictionary by

  var result = responseList["_links"];

How would I process this object variable further to retrieve href value of "account"? thank you in anticipation

Upvotes: 0

Views: 1349

Answers (2)

user6840349
user6840349

Reputation:

Why not use JObject to parse the JSON? I took a sample of your JSON and was able to parse the data into an object and use it like the following:

        JObject j = JObject.Parse(jsonData);

        Console.WriteLine(j["_links"]["account"]["href"]);

Upvotes: 1

Thiyagu Rajendran
Thiyagu Rajendran

Reputation: 655

You can use foreach iteration for nested Json

foreach(var outer in responseList)
{
foreach (var middle in (Dictionary<string, object>) outer.Value)
{
     foreach (var inner in (Dictionary<string, string>) middle.Value)
     {
       Jsondata data = new Jsondata();
       data.href = inner["href"]; 
     }      
}
}

public class Jsondata
{
 public string href { get; set; }
 public string type { get; set; }
 public string resource{ get; set; }
}

Sincerely,

Thiyagu Rajendran

**Please mark the replies as answers if they helps.

Upvotes: 2

Related Questions