Transforming C# array into JSON table.

I have a C# object, below is JSON representation of the object, and I need to build a "table" view that can be put into a grid where I essentially "transform" the Header array into columns and then populate the rows with the Rows array. As you can see, not all columns will contain data.  I will be using this with an AngularJS capable grid so the output will need to be JSON.

Any ideas how this can be accomplished using C#?

    {
       "Header":[
          "FILENAME",
          "DOC_NAME",
          "LASTREAD",
          "DATE",
          "PRINT_DATE",
          "CLI_PRINTDATE",
          "ISSUE",
          "SUBJECT",
          "EVENT_CODE",
          "CE_STATE",
          "DESIGNATION",
          "PROGRAM_CODE",
          "COURSE NAME",
          "RUN_DATE",
          "PROGRAM CODE",
          "DESTIBUTION",
          "EVENT CODE"
       ],
       "Rows":[
          [
             "/DownloadFile2.aspx?File=ReimbursementForm20130919.pdf",
             "Reimbursement Form",
             "",
             "",
             "20130909",
             "",
             "",
             "",
             "20130919",
             "",
             null,
             "RAR",
             "ARMS",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=ReimbursementForm20130814.pdf",
             "Reimbursement Form",
             "",
             "",
             "20130813",
             "",
             "",
             "",
             "20130814",
             "",
             null,
             "RCY",
             "RCRS",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=Dues07242013Dues.PDF",
             "Dues Invoice",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "20130724",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=07242013BenefitsLetter.PDF",
             "Benefits Letter",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "20130724",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=Summer_2013",
             "Mag Summer 2013",
             "",
             "",
             "",
             "",
             "Summer 2013",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=ID_CARD_999999.PDF",
             "ID Card",
             "",
             "",
             "20121102",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=Diploma999999++20121022.PDF",
             "Diploma",
             "",
             "",
             "20121022",
             "",
             "",
             "",
             "19990828",
             "",
             "",
             "",
             "CCI",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=CLtr20120928.pdf",
             "Confirmation Letter",
             "",
             "",
             "10042012",
             "",
             "",
             "",
             "20120928",
             "",
             "NCIM",
             "DPI",
             "DPI",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=PassLetter20081210.PDF",
             "Pass Letter",
             "",
             "",
             "20100318",
             "",
             "",
             "",
             "20081210",
             "",
             "SRM",
             "ADM",
             "ASR",
             "",
             "",
             "",
             ""
          ],
          [
             "/DownloadFile2.aspx?File=Certificate20091204.PDF",
             "Certificate",
             "",
             "",
             "20091214",
             "",
             "",
             "",
             "20091204",
             "AC",
             "",
             "",
             "RGS",
             "",
             "",
             "",
             ""
          ]
       ]
    }

Open in new window

LVL 27
Eddie ShipmanAll-around developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
What exactly do you need?
A way to generate this JSON from a CLR object?
0
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

You can create a class and assign this json after deserialization to the object. You can loop through the object Header and Rows to form a table.

public class RootObject
{
    public List<string> Header { get; set; }
    public List<List<string>> Rows { get; set; }
}

Open in new window

0
apeterCommented:
Please have a header class like below and

use the json dll from this url. http://json.codeplex.com/ and

samples of converting c# to json is here, http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx

Model
--------
public class TableView<H,R>
{
       public H Header { get; set; }
        public List<R> Rows { get; set; }
}
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Eddie ShipmanAll-around developerAuthor Commented:
I guess it is not really being understood what I need. I need something like this, showing for first two records only. I already have Newtonsoft's Json package installed.
{
  [
    "FILENAME":"/DownloadFile2.aspx?File=ReimbursementForm20130919.pdf",
    "DOC_NAME":"Reimbursement Form",
    "LASTREAD": "",
    "DATE": "",        
    "PRINT_DATE": "20130909",
    "CLI_PRINTDATE": "",        
    "ISSUE": "",        
    "SUBJECT": "",        
    "EVENT_CODE": "20130919",
    "CE_STATE": "",        
    "DESIGNATION": null,      
    "PROGRAM_CODE": "RAR",     
    "COURSE NAME": "ARMS",    
    "RUN_DATE": "",        
    "PROGRAM CODE": "",        
    "DESTIBUTION": "",        
    "EVENT CODE": ""         
  ],
  [
     "FILENAME": "/DownloadFile2.aspx?File=ReimbursementForm20130814.pdf",
     "DOC_NAME": "Reimbursement Form",
     "LASTREAD": "",
     "DATE": "",
     "PRINT_DATE": "20130813",
     "CLI_PRINTDATE": "",
     "ISSUE": "",
     "SUBJECT": "",
     "EVENT_CODE": "20130814",
     "CE_STATE": "",
     "DESIGNATION": null,
     "PROGRAM_CODE": "RCY",
     "COURSE NAME": "RCRS",
     "RUN_DATE": "",
     "PROGRAM CODE": "",
     "DESTIBUTION": "",
     "EVENT CODE": ""
  ]
}

Open in new window

0
Alexandre SimõesManager / Technology SpecialistCommented:
What you need is actually the inverse:
[
  {
    "FILENAME":"/DownloadFile2.aspx?File=ReimbursementForm20130919.pdf",
    "DOC_NAME":"Reimbursement Form",
    "LASTREAD": "",
    "DATE": "",        
    "PRINT_DATE": "20130909",
    "CLI_PRINTDATE": "",        
    "ISSUE": "",        
    "SUBJECT": "",        
    "EVENT_CODE": "20130919",
    "CE_STATE": "",        
    "DESIGNATION": null,      
    "PROGRAM_CODE": "RAR",     
    "COURSE NAME": "ARMS",    
    "RUN_DATE": "",        
    "PROGRAM CODE": "",        
    "DESTIBUTION": "",        
    "EVENT CODE": ""         
  },
  {
     "FILENAME": "/DownloadFile2.aspx?File=ReimbursementForm20130814.pdf",
     "DOC_NAME": "Reimbursement Form",
     "LASTREAD": "",
     "DATE": "",
     "PRINT_DATE": "20130813",
     "CLI_PRINTDATE": "",
     "ISSUE": "",
     "SUBJECT": "",
     "EVENT_CODE": "20130814",
     "CE_STATE": "",
     "DESIGNATION": null,
     "PROGRAM_CODE": "RCY",
     "COURSE NAME": "RCRS",
     "RUN_DATE": "",
     "PROGRAM CODE": "",
     "DESTIBUTION": "",
     "EVENT CODE": ""
  }
]

Open in new window

Am I right?
This is simply an array of an object.
public class myItem {
     public string FILENAME { get; set; }
     public string DOC_NAME{ get; set; }
     public string LASTREAD{ get; set; }
     public string LASTREAD{ get; set; }
     public string DATE{ get; set; }
     // and so on...
}

// the object you want to serialize to json is this
List<myItem> MyItems = new List<myItem>() { ... }

Open in new window

0
Eddie ShipmanAll-around developerAuthor Commented:
@Alex, no, it will be a JSON table to be used in an AngularJS capable grid.
0
Alexandre SimõesManager / Technology SpecialistCommented:
@EddieShipman: To what are you saying no?
Your JSON sample is not even valid JSON... mine is...

BTW, what's a JSON table?

From my JSON (modified from yours) you can generate the <table> markup and send it but it will be much fatter on the wire.
Send the JSON and generate the <table> on the client side, you will be sending a lot less data and you won't be mixing concerns.
Besides that, client computers are pretty powerful these days, send as much unprocessed data to them as you can. This way you're saving your server's resources to handle more requests.
0
Eddie ShipmanAll-around developerAuthor Commented:
This HAS to be dynamic, the Header may not always contain the same "column" names.
0
Eddie ShipmanAll-around developerAuthor Commented:
I have actually been successful at creating the JSON string myself:
string tbl = "[";
for (int i = 0; i <= Rows.Count()-1; i++)
{
    tbl = String.Concat(tbl, "{");
    for (int x = 0; x <= docs.Header.Count()-1; x++)
    {
        tbl = String.Concat(tbl, "\"", docs.Header[x], "\":\"", Rows[i][x], "\",");
    }
    tbl = String.Concat(tbl.TrimEnd(','), "},");
}
tbl = String.Concat(tbl.TrimEnd(','), "]");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
Ok, now I got what you want.
You need to generate json to feed this grid: http://angular-ui.github.io/ng-grid/

As the schema is variable it has to be done like you did.
0
Eddie ShipmanAll-around developerAuthor Commented:
Got it!
0
Alexandre SimõesManager / Technology SpecialistCommented:
For me you can accept your own solution and close this.

Sorry I didn't understand correctly what you actually needed.

Cheers!
0
Eddie ShipmanAll-around developerAuthor Commented:
Own Solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.