Dany Gagnon
Dany Gagnon

Reputation: 112

Adding dynamic object to json

first of all, thanks for reading. Furthermore, i need your help!

By the way, im using Newtonsoft.Json;

Console image

How can i add a string to this:

obj.namedTypes.Menu.fields.< string > = new ExpandoObject();

So i have this code at the moment:

internal class Program
{
    private static void Main(string[] args)
    {
        Regex reg = new Regex(":addSubMenu\\((?<Description>.+),[a-z A-Z](\'|\")(?<Identifier>[a-zA-Z]+)(\'|\")\\)"); //Regular expression for finding the id and description i want.

        dynamic obj = new ExpandoObject(); 

        obj.global = new ExpandoObject();
        obj.global.type = "table";
        obj.global.fields = new ExpandoObject();

        obj.global.fields.scriptConfig = new ExpandoObject();
        obj.global.fields.scriptConfig.type = "function";
        obj.global.fields.scriptConfig.args = new string[] {};
        obj.global.fields.scriptConfig.description = "a simple description";
        obj.global.fields.scriptConfig.returnTypes = new List<ExpandoObject>();

        dynamic arguments = new ExpandoObject();
        arguments.type = "ref";
        arguments.name = "Menu";
        obj.global.fields.scriptConfig.returnTypes.Add(arguments);

        obj.namedTypes = new ExpandoObject();

        obj.namedTypes.Menu = new ExpandoObject();
        obj.namedTypes.Menu.type = "table";
        obj.namedTypes.Menu.fields = new ExpandoObject();

        Console.WriteLine(JsonConvert.SerializeObject(obj, Formatting.Indented));

        MatchCollection matches = reg.Matches(File.ReadAllText("test.txt"));

        string description;
        string value;

        foreach (Match m in matches)
        {
            description = m.Groups["Description"].ToString();
            value = m.Groups["Identifier"].ToString();

            /* I want to add the value as a object like this: ->
             * obj.namedTypes.Menu.fields.<value> = new ExpandoObject();
             * obj.namedTypes.Menu.fields.<value>.description = description;
            */
            Console.WriteLine($"Description: {description}\nValue: {value}");
        }
        Console.ReadLine();
    }

I want to add the value as a object like this: ->

         obj.namedTypes.Menu.fields.<value> = new ExpandoObject();

         obj.namedTypes.Menu.fields.<value>.description = description;

Upvotes: 0

Views: 612

Answers (1)

Jayanta Patra
Jayanta Patra

Reputation: 56

Try using Dictionary.

Your modified code would be -

//Initialization of Dictonary
obj.namedTypes.Menu.fields = new Dictionary<string,string>();
//Reading from File and iteration on matches
MatchCollection matches = reg.Matches(File.ReadAllText("test.txt"));

string description;
string value;

foreach (Match m in matches)
{
   description = m.Groups["Description"].ToString();
   value = m.Groups["Identifier"].ToString();
   obj.namedTypes.Menu.fields.Add(description , value);
}

//End: Serialize the Whole stuff
Console.WriteLine(JsonConvert.SerializeObject(obj, Formatting.Indented));

Upvotes: 1

Related Questions