0

I have this code in c# that gives me a dictionary of my data by date, it works perfect except when the key is serialized into json. the json ends up to be just the toString() property of the class instead of the property name and value I expect. Items in the collection for each of the keys serialize fine. This is my code:

  list = m
  .GroupBy(x => new StatCallDateModel { DateText = x.DateText })
  .ToDictionary(x => x.Key, x => x.ToList()
  .ConvertAll(c => (object)new { Agent = c.Agent, Talk = c.Talk }));
   return list;

One thing I did notice was that when I add the date object to the collection under the key the json parses fine. It is just the fact that it is a key in a dictionary that makes it parse like this. I am using this code to form the json for d3 charts; so the format is important.

Is there a way around this or can I get json similar to this without a dictionary?

  • `return base.ToString().Replace(base.ToString(), string.Format(@"{{ ""DateText"":""{0}"", }}", this.DateText));` Did you think through what that code is doing, step by step? Anyway, if you're using json.NET, [this answer seems to address the problem you're having](http://stackoverflow.com/a/31412665/424129). – 15ee8f99-57ff-4f92-890c-b56153 May 03 '17 at 14:33
  • 1
    are you sure the json you want is a valid json?.You should have a name for your agent array like `"Agents" : [{ "Agent : "wade"` – caner May 03 '17 at 14:41
  • 1
    check your json for validation https://jsonlint.com/ – caner May 03 '17 at 14:45

1 Answers1

0

I found this post on stack overflow

It pointed me in the right direction, the short answer is a dictionary with a string for a key got me most of the way there...I just wanted to use the key as a property in the script in d3.

 List<object> ops = m.GroupBy(x => x.Agent).ToList()
 .ConvertAll(c => (object)new { Agent = c.First().Agent});
  list = m
 .GroupBy(x => string.Format(@"""DateText"": {0}", x.DateText ))
 .ToDictionary(x => x.Key, x => x.ToList()
 .ConvertAll(c => (object)new { Datetext = c.DateText,  Agent = c.Agent, 
  Talk = c.Talk  })));
  list.Add("keys", ops);

So this code is keyed by a date string and has the keys for the Agents in the final key of the dictionary. Without changing the sql at all.

Community
  • 1
  • 1