C# 需要帮助在c中将数据写入json吗#
我正在收集一些数据,并试图使用c#newtonsoft.json将这些数据写入json文件 将数据写入我的控制器中的Json文件时,我会陷入困境。 c#中的多维数组让我困惑 提前谢谢 这是我试图创建的Json文件的一个示例:C# 需要帮助在c中将数据写入json吗#,c#,asp.net,arrays,json,web-scraping,C#,Asp.net,Arrays,Json,Web Scraping,我正在收集一些数据,并试图使用c#newtonsoft.json将这些数据写入json文件 将数据写入我的控制器中的Json文件时,我会陷入困境。 c#中的多维数组让我困惑 提前谢谢 这是我试图创建的Json文件的一个示例: [ { "testmodule1": { "name": { "required": true, "options": [ "option1",
[
{
"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; }
}
公共类JsonData
{
公共字典模块名{get;set;}
}
公共类JsonParameters
{
公共JsonParametersData参数{get;set;}
}
公共类JsonParametersData
{
需要公共bool{get;set;}
公共列表选项{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);
}
}
公共类WebscrapeController:控制器
{
//获得:网络垃圾
公共行动结果索引()
{
列表数据=新列表();
data.Add(新的JsonData()
{
modulename=新字典()
{
Add(“testmodule1”,新的JsonParameters()
{
参数=新的JsonParametersData()
{
必需=真,
选项=新列表()
{
“选择1”,
“选择2”
}
}
})
}
});
字符串json=JsonConvert.SerializeObject(data.ToArray());
//将字符串写入文件
System.IO.File.writealText(
@“C:mypath”,
json);
}
}
请注意,属性名
“testmodule1”
和“testmodule2”
以及“name”
,“admin”
,“path”
,“server”
是任意的;由于属性名“testmodule1”
和“testmodule2”
以及“name”
、“admin”
、“path”
、“server”
和“port”
是任意的,事先不知道,您需要将结果
数组建模为列表
。这是因为,当使用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);
//使用集合初始值设定项语法为列表和字典分配结果。
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#collection-初始化者
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer
var results=新列表()
{
新字典()
{
{
“testmodule1”,
新字典()
{
{
“姓名”,
新JsonParametersData
{
必需=真,
选项=新列表(){“选项1”,“选项2”},
}
},
{
“管理员”,
新JsonParametersData
{
必需=真,
选项=新列表(){“选项1”,“选项2”},
}
},
{
“路径”,
新JsonParametersData
{
必需=真,
选项=新列表(){“选项1”,“选项2”},
}
}
}
},
}
};
var moduleName=“testmodule2”;
var moduleParameters=new[]{“name”、“server”、“port”};
//现在添加另一个结果,同时使用集合初始值设定项语法分配字典
结果.添加(新字典()
{
{
模块名称,
//循环遍历参数并将其转换为字典,
//其中,参数名是键,对应的JsonParametersData是值
模参数
.ToDictionary(n=>n,
n=>新的JsonParametersData
{
必需=真,
选项=新列表(){“选项1”,“选项2”},
})
}
});
var json=JsonConvert.serialized对象(结果、格式、缩进);
注:
- 有关如何将字典序列化为JSON的文档,请参阅和
- 我正在使用初始化最外层的
列表
- 我还使用集合初始值设定项语法初始化字典,如中所示
- 给定一组参数名称,以及获取每个参数的
的方法(问题中未显示),可以使用LINQ扩展方法从参数集合构造一个JsonParametersData
字典
工作示例.Net fiddle。这里是一种使用Newtonsoft JObject的不同方法
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用制度;
命名空间堆栈溢出
{
公共课程
{
公共静态void Main(字符串[]args)
{
JArray数组=新的JArray();
//单元1
JObject参数=新的JObject();
AddParameter(参数,“名称”,true,新[]{“选项1”,“选项2”});
AddParameter(参数“admin”,true,新[]{“option1”,“option2”});
AddParameter(参数,“路径”,false,新[]{“选项1”,“选项2”,“选项3”});
作业对象模块=
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace StackOverflow
{
public class Program
{
public static void Main(string[] args)
{
JArray array = new JArray();
// Module 1
JObject parameter = new JObject();
AddParameter(parameter, "name", true, new[] { "option1", "option2" });
AddParameter(parameter, "admin", true, new[] { "option1", "option2" });
AddParameter(parameter, "path", false, new[] { "option1", "option2", "option3" });
JObject module = new JObject();
module.Add("testmodule1", parameter);
array.Add(module);
// Module 2
parameter = new JObject();
AddParameter(parameter, "name", true, new[] { "option1", "option2" });
AddParameter(parameter, "server", false, Array.Empty<string>());
AddParameter(parameter, "port", true, new[] { "option1", "option2", "option3" });
module = new JObject();
module.Add("testmodule2", parameter);
array.Add(module);
// Display result
var json = array.ToString();
Console.WriteLine(json);
}
static void AddParameter(JObject jObject, string name, bool required, string[] options)
{
JObject parameterProperties = new JObject();
parameterProperties.Add("required", JToken.FromObject(required));
parameterProperties.Add("options", JToken.FromObject(options));
jObject.Add(name, parameterProperties);
}
}
}