0

Hey all I am trying to add (or merge if you want to call it that) a few List<IDictionary<string, object>> that I have created that look like this:

List<IDictionary<string, object>> eachTblColumnProperties = new List<IDictionary<string, object>>
{
   new Dictionary<string, object>
   {{ "title", "Type" },{"name", "Type" },{ "type", "text" },{ "width", "80" },{ "align", "left" },
   { "filtering", "true" },{ "visible", "true" },{ "sorting", "true" }},
   new Dictionary<string, object>
   {{ "title", "Description" },{"name", "Description" },{ "type", "text" },{ "width", "80" },{ "align", "left" },
   { "filtering", "true" },{ "visible", "true" },{ "sorting", "true" }},
   etc etc...........
};

I will have about 6 or so List< IDictionary< string, object>> that I wish to turn into JSON (using json.net).

So the JSON output of all of them would look like this:

Currently I am just returning 1 List< IDictionary< string, object>> called status:

return JsonConvert.SerializeObject(
    new { status = eachTblColumnProperties }, 
    Formatting.Indented);

And like looks like:

{ 
  "status" [{
     "title":"Type",
     "name":"Type",
     "type":"text",
     "width":"80",
     "align":"left",
     "filtering":"true",
     "visible":"true",
     "sorting":"true"
 },{
     "title":"Description",
     "name":"Description",
     "type":"text",
     "width":"80",
     "align":"left",
     "filtering":"true",
     "visible":"true",
     "sorting":"true"
 },{
   ... etc etc...
 }]
}

And that works just fine but I need to be able to send all my List< IDictionary< string, object>> within that one return call...

So something that looks like this:

 {
    "status": [{
            "title": "Type",
            "name": "Type",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        },
        {
            "title": "Description",
            "name": "Description",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        },{
          etc..etc...
        }
    ],
    "AnotherStatus": [{
            "title": "Type",
            "name": "Type",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        },
        {
            "title": "Description",
            "name": "Description",
            "type": "text",
            "width": "80",
            "align": "left",
            "filtering": "true",
            "visible": "true",
            "sorting": "true"
        }, {
          etc... etc...
        }
    ]
 }

enter image description here

StealthRT
  • 10,108
  • 40
  • 183
  • 342

2 Answers2

1

What you want is a Dictionary<string, List<Dictionary<string, object>>>

Example:

Dictionary<string, List<Dictionary<string, object>>> eachTblColumnProperties = new Dictionary<string, List<Dictionary<string, object>>>
{
    { "Status", new List<Dictionary<string, object>> 
                {
                    new Dictionary<string, object>
                    {
                        { "title", "Type" },
                        {"name", "Type" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    },
                    new Dictionary<string, object>
                    {
                        { "title", "Description" },
                        {"name", "Description" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    }
                }},
    { "AnotherStatus", new List<Dictionary<string, object>> 
                {
                    new Dictionary<string, object>
                    {
                        { "title", "Type" },
                        {"name", "Type" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    },
                    new Dictionary<string, object>
                    {
                        { "title", "Description" },
                        {"name", "Description" },
                        { "type", "text" },
                        { "width", "80" },
                        { "align", "left" },
                        { "filtering", "true" },
                        { "visible", "true" },
                        { "sorting", "true" }
                    }
                }}
};

This should result in the JSON you want.

EDIT forgot to put new before the List. Working fiddle here

maccettura
  • 10,514
  • 3
  • 28
  • 35
0

Are you asking for something like this?

public class MyReturnType
{
    public List<IDictionary<string, object>> Status { get; set; }
    public List<IDictionary<string, object>> SomethingElse { get; set; }
}

var myReturnType = new MyReturnType
{
    Status = statusList,
    SomethingElse = someOtherList
};

return JsonConvert.SerializeObject(myReturnType, Formatting.Indented);
StealthRT
  • 10,108
  • 40
  • 183
  • 342
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • Nice Camilo! Thanks for that. That looks like just what i need. – StealthRT Jun 23 '17 at 18:14
  • @StealthRT I downvoted because you used anonymous classes in your example, this answer makes a concrete class all for the purpose of serialization. Its overkill and restricting. – maccettura Jun 23 '17 at 18:21
  • @maccettura did the OP state they wanted it to be dynamic? No. Did the OP say he didn't want a concrete type? No. This could also be done without the class, using anonymous types, but remembering the structure in different methods would suck. – Camilo Terevinto Jun 23 '17 at 18:22
  • @CamiloTerevinto the OP clearly uses an anonymous object, its easy to infer from that they cannot or do not want to have a concrete representation. Especially when the problem at hand is just to add a key to json. – maccettura Jun 23 '17 at 18:23
  • @maccettura your answer locks the OP into putting objects relevants to each other, but that's not the case of my answer. You don't know what that JSON will be used for either – Camilo Terevinto Jun 23 '17 at 18:35