需要帮助将数据写入C#中的json

查吉尔·贝哈吉

我正在抓取一些数据,并尝试使用c#newtonsoft.Json将抓取的数据写入json文件。

将数据写入控制器中的Json文件时,我陷入了困境C#中的多维数组使我感到困惑。

提前致谢。

这是我尝试创建的Json文件的示例:

[
{
    "testmodule1": {
        "name": {
            "required": true,
            "options": [
                "option1",
                "option2"
            ]
        },
        "admin": {
            "required": true,
            "options": [
                "option1",
                "option2"
            ]
        },
        "path": {
            "required": true,
            "options": [
                "option1",
                "option2"
            ]
        }
    }
},
{
    "testmodule2": {
        "name": {
            "required": true,
            "options": [
                "option1",
                "option2"
            ]
        },
        "server": {
            "required": false,
            "options": [
            ]
        },
        "port": {
            "required": true,
            "options": [
                "option1",
                "option2"
            ]
        }
    }
}
]

这些是我的课程:

    public class JsonData
{
    public Dictionary<string, JsonParameters> modulename { get; set; }
}

public class JsonParameters
{
    public JsonParametersData parameter { get; set; }
}
public class JsonParametersData
{
    public bool required { get; set; }
    public List<string> options { get; set; }
}

这是我的控制器,这是我卡住的地方。名称modulename在当前上下文中不存在

public class WebscrapeController : Controller
    {
        // GET: Webscrape
        public ActionResult Index()
        {

            List<JsonData> data = new List<JsonData>();
            data.Add(new JsonData()
            {
                modulename = new Dictionary<string, JsonParameters>()
                {
                    modulename.Add("testmodule1", new JsonParameters()
                    {
                        parameter = new JsonParametersData()
                        {
                            required = true,
                            options = new List<string>()
                            {
                                "option1",
                                "option2"
                            }
                        }
                    })
                }
            });

            string json = JsonConvert.SerializeObject(data.ToArray());

            //write string to file
            System.IO.File.WriteAllText(
                @"C:mypath",
                json);
        }
    }

在此处输入图片说明

请注意,属性名"testmodule1""testmodule2"以及"name""admin""path""server"是任意的; 它们对于每个数组都不同。

数据库

由于属性名称"testmodule1""testmodule2"以及"name""admin""path""server""port"是任意的,事先不知道,你需要你的模型results阵列作为List<Dictionary<string, Dictionary<string, JsonParametersData>>>这是因为,当使用Json.NET将字典序列化为JSON时,字典键成为JSON属性名称。

因此,可以如下创建上述JSON:

// Allocate results using collection initializer syntax for the lists and for the dictionaries.
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#collection-initializers
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer
var results = new List<Dictionary<string, Dictionary<string, JsonParametersData>>>()
{
    new Dictionary<string, Dictionary<string, JsonParametersData>>()
    {
        {
            "testmodule1",
            new Dictionary<string, JsonParametersData>()
            {
                {
                    "name",
                    new JsonParametersData
                    {
                        required = true,
                        options = new List<string>() { "option1", "option2" },
                    }
                },
                {
                    "admin",
                    new JsonParametersData
                    {
                        required = true,
                        options = new List<string>() { "option1", "option2" },
                    }
                },
                {
                    "path",
                    new JsonParametersData
                    {
                        required = true,
                        options = new List<string>() { "option1", "option2" },
                    }
                }
            }
        },
    }
};

var moduleName = "testmodule2";
var moduleParameters = new [] { "name", "server", "port" };         

// Now add another result, allocating the dictionary with collection initializer syntax also
results.Add(new Dictionary<string, Dictionary<string, JsonParametersData>>()
    {
        {
            moduleName,
            // Loop through the parameters and convert them to a dictionary,
            // where the parameter name is the key and the corresponding JsonParametersData is the value
            moduleParameters
                .ToDictionary(n => n,
                              n => new JsonParametersData
                              {
                                  required = true, 
                                  options = new List<string>() { "option1", "option2" },
                              })
        }
    });

var json = JsonConvert.SerializeObject(results, Formatting.Indented);

笔记:

工作样本.Net在这里摆弄

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章