0
首先感谢阅读。此外,我需要你的帮助!向json添加动态对象
顺便说一下,即时通讯使用Newtonsoft.Json;
我怎么才能将字符串添加到这一点:
obj.namedTypes.Menu.fields。 < string> = new ExpandoObject();
所以我有这样的代码的时刻:
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();
}
我要添加值作为这样一个对象: - >
obj.namedTypes.Menu.fields.<value> = new ExpandoObject();
obj.namedTypes.Menu.fields.<value>.description = description;