0

I have a Dictionary<string, object> advertiserResponse that looks like this...

"AdvertiserLookup": {
          "Agency": {
            "AgencyAlpha": "ABC",
            "Media": [
              {
                "Code": "123",
                "Name": "XYZ",
                "Advertisers": {
                  "Advertiser": [
                    {
                      "Code": "JKL",
                      "Name": "EFG",
                      "BusinessKey": "HIJ"
                    },
                    {
                      "Code": "KLM",
                      "Name": "NOP",
                      "BusinessKey": "QRS"
                    },
                    {

This is the line I'm using to try to write to a text file:

File.WriteAllLines("test.txt", advertiserResponse.Select(x => "[" + x.Key + " " + x.Value + "]").ToArray());

I'm expecting the Json (not necessary in pretty print) with all keys and text below AdvertiserLookup. What I get in test.txt:

[AdvertiserLookup System.Collections.Generic.Dictionary``2[System.String,System.Object]]

How do I write the entire thing to text?

Kyle
  • 5,407
  • 6
  • 32
  • 47
  • I guess you noticed that this is hierarchical, no? – BlueM Apr 07 '14 at 20:44
  • 1
    The Dictionary can't looks like this, it is just JSON representation of the data contained in Dictionary. You can save the data as JSON before converting it to Dictionary. – Hamlet Hakobyan Apr 07 '14 at 20:44
  • 3
    I propose to parse this as JSON and use the proper internal representation for it instead Dictionary. http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c You can then easily dump out the data into a string like in the linked example `JsonConvert.SerializeObject(product);` – BlueM Apr 07 '14 at 20:44

1 Answers1

2

Give Json.NET a try. You should be able to serialize your dictionary.

var jsonString = JsonConvert.SerializeObject(advertiserResponse, Formatting.Indented)
BlueM
  • 6,523
  • 1
  • 25
  • 30