我需要从这个 JSON 中过滤所有用户组
I need to filter all the userGroups from this JSON
TaskProperty.InternalAppConfiguration 定义了一个包含各种信息的 JSON,我需要从这个 JSON 中 获取所有用户组 。我只需要用户组的名称。
// saving task properties
foreach (var TaskProperty in representation.TaskProperties != null ? representation.TaskProperties : new List<TaskProperty>())
{
await this.taskRepository.AddTaskPropertyAsync(new TaskProperty()
{
ConnectionId = TaskProperty.ConnectionId,
NodeId = result.VertexIdPairs[Convert.ToInt16(TaskProperty.NodeId)],
TaskId = TaskProperty.TaskId,
InternalAppConfiguration = TaskProperty.InternalAppConfiguration,
IsApprovalTask = TaskProperty.IsApprovalTask
});
};
我的 JSON 看起来像这样,
在 JSON 可视化工具中看起来像这样
如何将所有用户组存储到单个数组或列表或我将来可以使用的东西中?
我尝试使用 Newtonsoft 进行反序列化,但我无能为力。请帮帮我。我非常感谢您的回答和建议。谢谢!
使用 Newtonsoft,这就是我要做的:
public static IEnumerable<string> GetNamesFromJson(string json)
{
InternalAppConfiguration jsonObject = JsonConvert.DeserializeObject<InternalAppConfiguration>(json);
return jsonObject.OptionsData.UserGroups.Select(ug => ug.DisplayName);
}
public class InternalAppConfiguration
{
public OptionsData OptionsData { get; set; }
}
public class OptionsData
{
public List<UserGroup> UserGroups { get; set; }
}
public class UserGroup
{
public string DisplayName { get; set; }
}
TaskProperty.InternalAppConfiguration 定义了一个包含各种信息的 JSON,我需要从这个 JSON 中 获取所有用户组 。我只需要用户组的名称。
// saving task properties
foreach (var TaskProperty in representation.TaskProperties != null ? representation.TaskProperties : new List<TaskProperty>())
{
await this.taskRepository.AddTaskPropertyAsync(new TaskProperty()
{
ConnectionId = TaskProperty.ConnectionId,
NodeId = result.VertexIdPairs[Convert.ToInt16(TaskProperty.NodeId)],
TaskId = TaskProperty.TaskId,
InternalAppConfiguration = TaskProperty.InternalAppConfiguration,
IsApprovalTask = TaskProperty.IsApprovalTask
});
};
我的 JSON 看起来像这样,
在 JSON 可视化工具中看起来像这样
如何将所有用户组存储到单个数组或列表或我将来可以使用的东西中? 我尝试使用 Newtonsoft 进行反序列化,但我无能为力。请帮帮我。我非常感谢您的回答和建议。谢谢!
使用 Newtonsoft,这就是我要做的:
public static IEnumerable<string> GetNamesFromJson(string json)
{
InternalAppConfiguration jsonObject = JsonConvert.DeserializeObject<InternalAppConfiguration>(json);
return jsonObject.OptionsData.UserGroups.Select(ug => ug.DisplayName);
}
public class InternalAppConfiguration
{
public OptionsData OptionsData { get; set; }
}
public class OptionsData
{
public List<UserGroup> UserGroups { get; set; }
}
public class UserGroup
{
public string DisplayName { get; set; }
}