0

I want to iterate through a 2D Array contents[,], and build up a JSON-like object as a do so. The structure is like below. This will then be sent to a server endpoint, processed by a server-side function and stored into a database.

In JavaScript this would have been straightforward, but that is because typing is dynamic. As far as I can tell, I have two options for building up sem-structured data in C#:

  1. Create a model as mentioned here: Convert C# Object to Json Object and then convert to JSON
  2. Build up a nested hashtable (not dictionary because I won't know types of the values until I need to build the object)

What is the normal way of doing this in C#? I have also come across the term 'POCO', which seems to somewhat correspond to point 1.?

[
    {
        tableName: someName,
        fields: [ordered, list, of, field, names],
        values: [
            [ordered, list, of, cell, row, values],
            [ordered, list, of, cell, row, values],
            [ordered, list, of, cell, row, values]
        ]
    },
    {
        tableName: someName,
        fields: [ordered, list, of, field, names],
        values: [
            [ordered, list, of, cell, row, values],
            [ordered, list, of, cell, row, values],
            [ordered, list, of, cell, row, values]
        ]
    },
    etc
]
Community
  • 1
  • 1
Zach Smith
  • 8,458
  • 13
  • 59
  • 133
  • 2
    c# has dynamic types: https://msdn.microsoft.com/en-us/library/dd264736.aspx And the JSON.Net library is widely used for manipulating JSON: http://www.newtonsoft.com/json (3,120,111 downloads of the latest version via nuget). As for what approach is best for you, I can't answer that as the question is way too broad and I don't know the work you're doing, the data your using, or what your personal preferences are. There are a lot of approaches that could be taken. –  Jan 11 '17 at 12:14
  • 1
    What part of the structure is dynamic? This looks very structured to me. You have a table with a name which has a list of fields and the data which will be placed into these fields. Is it simply that the values of the data are different types? Or am I missing something? – jason.kaisersmith Jan 11 '17 at 12:24
  • Yes. Only the values are of different types – Zach Smith Jan 11 '17 at 12:26
  • Coming from JavaScript, the concept of objects is slightly different since you can build them up directly. C# on the other seems to require a template. I think an answer to this question could be beneficial for someone without a background in OO languages that use a class-based architecture. – Zach Smith Jan 11 '17 at 12:34
  • If only the values are different types then just use an "Object" type. – jason.kaisersmith Jan 11 '17 at 13:56

1 Answers1

1

It seems that your structure is rigid and that only the data values are different, in this case you should just use an object type for your values. Your class could then look something like this;

class MyObject
{
    String Tablename;
    List<Field> myFields = new List<Field>();
    List<object> values = new List<object>();
}

Or you could just create a Json Object directly and parse that if you need to retrieve any additional data.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51