这是我从 (.)json 文件中读取的 Json。
{
"steps": [
{
"stepType": "runFolderUpdate",
"stepData": {
"actionType": "update",
"folderData": {
"folderName": "New Folder 1",
"dirName": "C:/demo/demo.xml",
"machineAddress": "10.23.44.12"
}
}
},
{
"stepType": "runFolderCreate",
"stepData": {
"actionType": "create",
"actionData": {
"folderName": "New Folder 2",
"dirName": "C:/Demo",
"machineAddress": "10.23.211.2"
}
}
},
{ . . . },
{ . . . }
]
}
我的要求是从此 Json 中获取一个数组,以便我可以拥有所有字段并可以在“stepType”的帮助下访问它,并进一步在“actionType”的帮助下访问它”值。
对于stepType =“runFolderUpdate”
{
"stepType": "runFolderUpdate",
"stepData": {
"actionType": "update",
"folderData": {
"folderName": "New Folder 1",
"dirName": "C:/demo/demo.xml",
"machineAddress": "10.23.44.12"
}
}
}
对于stepType =“runFolderCreate”
{
"stepType": "runFolderCreate",
"stepData": {
"actionType": "create",
"actionData": {
"folderName": "New Folder 2",
"dirName": "C:/Demo",
"machineAddress": "10.23.211.2"
}
}
}
现在我有两个 block ,一个用于创建,一个用于更新,我可以继续根据要求访问值,并且不受 JSON 键排列方式的限制。
我尝试使用 Newtonsoft 库中的 JsonReader 来完成此操作,但问题是它只是向前移动的阅读器,我无法返回。现在,由于我们正在讨论这是一个 JSON 文件,因此键的放置顺序并不重要,但使用 JsonReader 我就束手无策了。
举例来说,如果stepType低于stepData,那么我无法使用 JsonReader 返回到stepData,因为我知道我是什么类型的stepType正在谈论。
我正在寻找一种方法,如何将这个 steps 对象 Json 转换为数组,每个 blob 将充当我可以访问的信息 block (就像我们在数组中所做的那样,使用索引,这样我就不必担心键的顺序。
////////更新/////////
我正在尝试做这样的事情......
JObject object = Read Json from file...
JArray array = object.get("steps");
现在我有了数组,基于我可以处理的stepType...
这可能吗?
最佳答案
您可以使用自定义转换器相当轻松地将所有这些转换为 C# 类。 Newtonsoft 提供了一些非常有用的扩展点。因此,假设您有以下类结构:
public class Root
{
public List<Step> Steps { get; set; }
}
// Here we are telling the serialiser to use the converter
[JsonConverter(typeof(StepConverter))]
public class Step
{
public string StepType { get; set; }
public IStepData StepData { get; set; }
}
public interface IStepData
{
string ActionType { get; set; }
}
public class RunFolderUpdate : IStepData
{
public string ActionType { get; set; }
//etc - you can fill in the rest here
}
public class RunFolderCreate : IStepData
{
public string ActionType { get; set; }
//etc - you can fill in the rest here
}
现在我们可以像这样实现转换器:
public class StepConverter : JsonConverter<Step>
{
public override Step ReadJson(JsonReader reader, Type objectType,
[AllowNull] Step existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var step = JObject.ReadFrom(reader);
var stepType = step["stepType"].Value<string>();
switch(stepType)
{
case "runFolderUpdate":
return new Step
{
StepType = stepType,
StepData = step["stepData"].ToObject<RunFolderUpdate>()
};
case "runFolderCreate":
return new Step
{
StepType = stepType,
StepData = step["stepData"].ToObject<RunFolderCreate>()
};
}
throw new Exception("Errr, unknown step type!");
}
public override void WriteJson(JsonWriter writer, [AllowNull] Step value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
最后你可以像这样反序列化:
var result = JsonConvert.DeserializeObject<Root>(json);
关于c# - 将 Json 对象转换为 Json 数组(不使用 JsonReader),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67486988/