我需要从这个JSON中筛选出所有的用户群。

问题描述 投票:0回答:1

TaskProperty.InternalAppConfiguration定义了一个包含各种信息的JSON,而我需要的是 获取所有用户组 从这个JSON中。我只需要userGroups的名字。

                    // 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格式是这样的。

enter image description here

在JSON可视化器中,它看起来像这样

enter image description here

我如何将所有的用户群存储到一个数组或列表中,或者其他我可以在未来使用的东西?我已经尝试使用Newtonsoft进行反序列化,但我无法做任何事情,请帮助我。我真的很感谢你的回答和建议。谢谢您

c# .net json .net-core json-deserialization
1个回答
1
投票

使用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; }
    }
© www.soinside.com 2019 - 2024. All rights reserved.