0

I am trying to implement a PHP function I have already made in C# but I have no idea of the syntax used in C# used to Navigate Dictionaries! (Associative arrays in PHP). What I am doing essentially is making a foreach loop to print each instance of a property in the associative array. I'm not sure if my question is completely clear so to make it easier, What would be the C# equivalent of this?

foreach ( $data->response->docs as $info )
{
    echo "{$info->EventId},";
    echo "{$info->VenueName},";
}

I just need a nudge in the right direction syntax wise.

Thanks

EDIT-

oops, when I posted this question I was tired. my problem is how to navigate a dictionary serialized from JSON. in my example I was parsing two properties from this sample data :-

{"responseHeader":{"status":0,"QTime":2},"response":{"facet_counts":{},"numFound":110,"docs":[{"VenueSEOLink":"/Aberdeen-Music-Hall-tickets-Aberdeen/venue/443660","VenueId":"10512085..... 

etc etc....

So I am trying to figure out how to go down multiple levels through the dict. (C# equivalent of ->)

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105

1 Answers1

0

In C#, you can use a System.Collections.Dictionary or a generic System.Collections.Generic.Dictionary<TKey, TObject>. I recommend the generic dictionary because it is strongly-typed and you don't have to cast the contents every time you retrieve an object.

Assuming your EventId is an int, you can access it like this:

Dictionary<int, string> info = new Dictionary<int, string>();
int eventId = 42;
info[eventId] = "Some value";

var value = info[eventId];
// value = "Some value" as string

If you are using string keys just change the dictionary definition and key type from int to string.

To iterate on a dictionary (or any collection or other object that implements IEnumerable), you use essentially the same syntax as in PHP:

foreach (var item in info) {
  // Do something with item
}

More info Systems.Collections.Generic.IDictionary is here.

Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
  • Try [this desrcription of converting json to C#](http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object) or [this description of parsing json in C#](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) – Peter Gluck Sep 09 '12 at 22:14