c# - 需要帮助在 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);
笔记:
有关如何将字典序列化为 JSON 的文档,请参阅序列化字典和序列化指南:字典和哈希表。
我正在
List<T>
使用集合初始化语法初始化最外层。我还使用集合初始化器语法初始化字典,如如何:使用集合初始化器初始化字典(C# 编程指南)中所示。
给定参数名称的集合和获取
JsonParametersData
每个参数名称的方法(问题中未显示),LINQ 扩展方法Enumerable.ToDictionary<TSource, TKey, TElement>()
可用于Dictionary<string, JsonParametersData>
从参数集合构造 a 。
工作示例 .Net在这里摆弄。
推荐阅读
- sql - oracle中如何修改OBJECT TYPE TABLE的大小
- c# - 如何获取参数的名称?
- python - 在三个 DataFrame 中寻找复合键的存在,并相应地连接 DataFrame
- servicestack - ServiceStack 没有反序列化字典
适当地 - go - 在具有特定本地地址的 GoLang 中使用 Dial
- javascript - html2canvas 选项类型:'view' 仍然呈现全身
- string - 从可变字符串长度中删除(减去)最后 n 个字符
- python - Python项目的依赖控制
- plot - JULIA 语言中的原生 PGFPlots 改变字体大小
- linux - UDP communication is lost on linux, are "UDP sockets" managed differentely on linux compared to windows when using C# .NET Core 2.1?