0

Being a VB.Net programmer using VB.Net 2015 community, I come across items in C# that I need to convert to VB, but this time I don't understand what I'm working with. The website service I'm consuming returns and expects JSON / JOBJECTS structures like:

var token = new {
    iss = PARTNERID,
    product = "twpemp",
    sub = "partner",
    siteInfo = new {
        type = "id",
        id = SITEID
    },
    exp = (Int32)DateTime.UtcNow.Add(new TimeSpan(0, 4, 30)).Subtract(new DateTime(1970, 1, 1)).TotalSeconds
};

An online converter converted this into:

Dim EPochTime = DateTime.UtcNow.Add(New TimeSpan(0, 4, 0)).Subtract(New DateTime(1970, 1, 1)).TotalSeconds
Dim Token = New With {
    Key .iss = AccNumber,
    Key .product = "twppartner",
    Key .sub = "partner",
    Key .siteInfo = New With {
        Key .type = "id",
        Key .id = Site},
    Key .exp = EPochTime
}

I need to dynamically create this type of structures because the "key name" and values change depending on what was returned and needs to be sent back. For example, depending on the siteid from above, the returning structure might have stuff like:

"Results": [
{
  "RecordNumber": 000001,
  "EmployeeCode": "0001",
  "FirstName": "John",
  "MiddleName": "A",
  "LastName": "Dow",
  "Designation": "Worker",
  "Home1": "Press",
},
{
  "RecordNumber": 000002,
  "EmployeeCode": "0002",
  "FirstName": "Jane",
  "MiddleName": "b",
  "LastName": "Dow",
  "Designation": "Helper",
  "Home1": "Office",
}
}

For the next client I submit a query for, and eventually need to update with might have:

"Results": [
{
  "RecordNumber": 12345,
  "EmployeeCode": "231",
  "FirstName": "Erick",
  "MiddleName": "G",
  "LastName": "Smith",
  "Department": "Electrial",
},
{
  "RecordNumber": 732456,
  "EmployeeCode": "853",
  "FirstName": "Fred",
  "MiddleName": "W",
  "LastName": "Kerber",
  "Department": "Electrial",
}
}

The difference between the two is one has "Department" and the other doesn't. This structure changes based on the siteID from the first call.

My main question is how do I create something like this dynamically in VB.NET, and secondarily, exactly what is this type of thing called? I'm calling it a structure for lack of better words.

3
  • Your second block is already creating it dynamically in VB, so whats the problem? Does the serialized result match what you need to send? They are called anonymous types Commented Jun 8, 2017 at 14:31
  • The key names change from siteID to siteID. Thanks for the type name, I'll research more on it! Fred Commented Jun 8, 2017 at 14:42
  • So what? You just type in different property names based on what you need. Based and the second part since you want an array of them (apparently), I just use a pair of concrete classes - maybe which inherit from a class with the shared parts. You can conditionally create a collection of type A or type B as easily as creating anonymous types Commented Jun 8, 2017 at 14:45

1 Answer 1

1

If you want a little more flexibility with outputting this stuff in JSON, there are two approaches:

1) You can use Dictionary<string,object> instead of dynamic types. With the dictionary approach, you can add (or exclude) properties at run-time. The JSON serializer will output in the same fashion as if you were serializing a dynamic type.

 var dict = new Dictionary<string, object>() { { "key1", "value1"} };
 dict["key2"] = DateTime.Now();
 dict["key3"] = 1234567;
 if (someCondition){
       dict["key4"] = new Dictionary<string, object>() { { "key5", "value5"}, { "key6", "value6"}};
 }

2) You can create a class that has ALL the available properties that the JSON structure might include. For optional properties which are numeric types, make them nullable:

public class Qwijibo 
{
    public int? RecordNumber {get;set;}
    public string EmployeeCode  {get;set;}
    public string FirstName {get;set;}
    public string MiddleName {get;set;}
    public string LastName {get;set;}
    public string Designation {get;set;}
    public string Home1 {get;set;}
    public string Department {get;set;
}

The above class may work in both the scenarios you presented. Properties you don't assign a value to will serialize as null in JSON. As long as whatever you're transmitting to doesn't get hung up on null values, you're good to go.

Sign up to request clarification or add additional context in comments.

1 Comment

After researching and hitting road blocks, I ended up using the dictionary method, which when combined with the Newtonsoft.Json.JsonConvert.SerializeObject function, I was able to read the schema, create / modify data as needed. Awesome answers! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.