1

I need the key and value data from SharedToDomains and SharedFromDomains. I want to print the values.

var LogResponse = DeserializeFromJson<AttributeContainer>(sLogResponse);

public class AttributeContainer
{
    public Dictionary<string, int> MimeTypes { get; set; }
    public Dictionary<string, Domain> SharedToDomains { get; set; }
    public Dictionary<string, Domain> SharedFromDomains { get; set; }
}
arun
  • 323
  • 6
  • 17
  • having Json arrays, i want like this LogResponse.SharedFromDomains["Value"].Documents, – arun Dec 11 '13 at 15:22

3 Answers3

2
SharedToDomains.Select(x => x.Value).ToList().ForEach(x => Console.WriteLine(x));
Konstantin
  • 3,254
  • 15
  • 20
  • if you need key also use `SharedToDomains.ToList().ForEach(x => Console.WriteLine(x.Key + "|" + x.Value));` – Konstantin Dec 11 '13 at 15:29
  • hi iwant Add key values in sb.Append("{"); sb.AppendFormat(@"""DT_RowId"": ""{0}""", 1); sb.Append(","); sb.AppendFormat(@"""DT_RowClass"": ""{0}""", rowClass); sb.Append(","); sb.AppendFormat(@"""0"": ""{0}""", "
    " + key + "
    "); sb.Append(","); sb.AppendFormat(@"""1"": ""{0}""", "
    " + key + "
    "); sb.Append("},");
    – arun Dec 11 '13 at 15:45
  • http://stackoverflow.com/questions/20497054/deserialize-json-using-json-net i am using this code , i want result for that to fetch the values. – arun Dec 12 '13 at 04:56
1

A quick way:

 foreach( var s in SharedToDomains.Keys )
 {
       string key = s;
       string val = SharedToDomains[s].ToString();
 }

If you really want to use linq:

var outputList = from s in SharedToDomains select new { key=s.Key, value=s.Value };
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
1

Loop over the value collection of the Dictionary and print the domain to the screen. I've put it in a method so you can re-use it later on:

private void PrintValues(Dictionary<string, Domain> dict)
{
    foreach(KeyValuePair<string, Domain> kvp in dict)
    {
        Console.WriteLine(kvp.Key); //Key
        Console.WriteLine(kvp.Value.ToString()); //Value
    }
}

//Usage:
PrintValues(SharedToDomains);
PrintValues(SharedFromDomains);
Abbas
  • 14,186
  • 6
  • 41
  • 72
  • A method for this is a bit overkill, as is the use of KeyValuePair. – Captain Skyhawk Dec 11 '13 at 15:27
  • I first used a loop over the ValueCollection but then saw on your answer that he also wants the Key. That's why I changed it. And if you're going to use the same code more than once, a method is always a good idea. :) – Abbas Dec 11 '13 at 15:28
  • http://stackoverflow.com/questions/20497054/deserialize-json-using-json-net i am using this code , i want result for that to fetch the values. – arun Dec 12 '13 at 04:55