C# 向json添加动态对象

C# 向json添加动态对象,c#,.net,json,json.net,C#,.net,Json,Json.net,首先,谢谢你的阅读。此外,我需要你的帮助 顺便说一下,我使用的是Newtonsoft.Json 如何将字符串添加到此中: obj.namedTypes.Menu.fields.=newexpandoobject() 所以我现在有这个代码: internal class Program { private static void Main(string[] args) { Regex reg = new Regex(":addSubMenu\\((?<Des

首先,谢谢你的阅读。此外,我需要你的帮助

顺便说一下,我使用的是Newtonsoft.Json

如何将字符串添加到此中:

obj.namedTypes.Menu.fields.=newexpandoobject()

所以我现在有这个代码:

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();
    }
内部类程序
{
私有静态void Main(字符串[]args)
{
Regex reg=new Regex(“:addSubMenu\\(?。+),[a-z a-z](\'|\”([a-zA-z]+)(\'|\”)\”);//用于查找所需id和说明的正则表达式。
动态对象=新的ExpandooObject();
obj.global=新的ExpandoObject();
obj.global.type=“表格”;
obj.global.fields=新的ExpandooObject();
obj.global.fields.scriptConfig=new ExpandoObject();
obj.global.fields.scriptConfig.type=“函数”;
obj.global.fields.scriptConfig.args=新字符串[]{};
obj.global.fields.scriptConfig.description=“简单描述”;
obj.global.fields.scriptConfig.returnTypes=新列表();
动态参数=新的ExpandooObject();
arguments.type=“ref”;
arguments.name=“菜单”;
obj.global.fields.scriptConfig.returnTypes.Add(参数);
obj.namedTypes=新的ExpandooObject();
obj.namedTypes.Menu=新建ExpandooObject();
obj.namedTypes.Menu.type=“表格”;
obj.namedTypes.Menu.fields=new ExpandoObject();
WriteLine(JsonConvert.SerializeObject(obj,Formatting.Indented));
MatchCollection matches=reg.matches(File.ReadAllText(“test.txt”);
字符串描述;
字符串值;
foreach(匹配中的匹配m)
{
description=m.Groups[“description”].ToString();
value=m.Groups[“Identifier”].ToString();
/*我想像这样添加对象的值:->
*obj.namedTypes.Menu.fields.=新的ExpandooObject();
*obj.namedTypes.Menu.fields..description=说明;
*/
WriteLine($“Description:{Description}\n值:{value}”);
}
Console.ReadLine();
}
我想像这样添加对象的值:->

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

         obj.namedTypes.Menu.fields.<value>.description = description;
obj.namedTypes.Menu.fields.=new ExpandoObject();
obj.namedTypes.Menu.fields..description=说明;
尝试使用字典

您修改的代码将是-

//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));
//命令的初始化
obj.namedTypes.Menu.fields=新字典();
//从文件读取并对匹配项进行迭代
MatchCollection matches=reg.matches(File.ReadAllText(“test.txt”);
字符串描述;
字符串值;
foreach(匹配中的匹配m)
{
description=m.Groups[“description”].ToString();
value=m.Groups[“Identifier”].ToString();
obj.namedTypes.Menu.fields.Add(描述、值);
}
//结束:序列化全部内容
WriteLine(JsonConvert.SerializeObject(obj,Formatting.Indented));