【问题标题】:I need to filter all the userGroups from this JSON我需要从这个 JSON 中过滤所有的用户组
【发布时间】:2020-06-03 05:41:00
【问题描述】:

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 是这样的,

在 JSON 可视化工具中看起来像这样

如何将所有用户组存储到单个数组或列表或我将来可以使用的东西中? 我尝试使用 Newtonsoft 反序列化,但我无能为力。请帮帮我。我非常感谢您的回答和建议。谢谢!

【问题讨论】:

  • 请告知您使用 Newtonsoft 尝试了多少!
  • @mahesh_b 我不知道如何格式化我的助手类来映射这个 JSON..

标签: c# .net json .net-core json-deserialization


【解决方案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; }
    }

【讨论】:

  • 它对我有用.. 非常感谢!我真的很感激@devcrp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 2018-10-08
  • 2012-05-14
  • 1970-01-01
相关资源
最近更新 更多