【问题标题】:How to convert a C# dictionary to a collection如何将 C# 字典转换为集合
【发布时间】:2022-01-16 22:44:00
【问题描述】:

我有一个 C# 字典列表如下:

"additionalDataList": [
                {
                    "key": "notes",
                    "value": "haasdas\n\ns"
                },
                {
                    "key": "twigTemplateId",
                    "value": "2"
                },
                {
                    "key": "AssignedUser",
                    "value": {
                        "Name": "To, To",
                        "Id": 108
                    }
                },
                {
                    "key": "assignedUserId",
                    "value": "114"
                }
            ],

但是,我需要这样转换:

"additionalDataList": 
                    {
                     
                        "notes": "haasdas\n\ns"
                    },
                    {
                
                        "twigTemplateId": "2"
                    },
                    {
                       
                        "AssignedUser": {
                            "Name": "To, To",
                            "Id": 108
                        }
                    },
                    {
                       
                        "assignedUserId": "114"
                    }
                

我怎样才能得到预期的结果?我尝试在 List Object 中进行转换,但没有成功。

更新: 我拥有的代码:

 public List<KeyValuePair<string, object>> AdditionalDataList { get; set; }

        public string AdditionalData {
            get
            {
                return _additionalData;
            }
            set
            {   if (!(value is null))
                {
                    AdditionalDataList = ParseJson(value).ToList();
                }
                _additionalData = value;
            }
        }

        public Dictionary<string, object> ParseJson(string json)
        {
            var dict = new Dictionary<string, object>();
            if (json is null)
            {
                return dict;
            }

            var obj = JObject.Parse(json);

            foreach (var property in obj)
            {
                var name = property.Key;
                var value = property.Value;

                if (value is JArray)
                {
                    dict.Add(name, value.ToArray());
                }
                else if (value is JValue)
                {
                    dict.Add(name, value.ToString());
                }
                else if (value is JObject)
                {
                    dict.Add(name, JObject.Parse(value.ToString()));
                }
                else
                {
                    throw new NotSupportedException("Invalid JSON token type.");
                }
            }

            return dict;
        }

我需要转换以下 JSON 字符串:(这已经在附加数据中)

{"notes":"haasdas\n\ns","twigTemplateId":2,"AssignedUser":{"Name":"To, To","Id":108},"assignedUserId":114}

【问题讨论】:

  • 你能显示一些代码吗?您是否有一些未序列化为正确 json 的数据层次结构?
  • 首先你需要将你的 json 反序列化为数据。其次,您需要对该数据进行一些投影,最后将其序列化回 json。那么您已经尝试了哪些方法,具体在哪里需要我们的帮助?
  • 我已经更新了代码。请检查。 @GuruStron
  • 标题中collection这个词的用法不正确。
  • AdditionalDataList的类型更改为Dictionary&lt;string, object&gt;

标签: c# json


【解决方案1】:

为您的属性键入 List&lt;KeyValuePair&lt;string, object&gt;&gt; AdditionalDataList 不代表您想要的 json。通用约定是将Dictionary 序列化为json 对象,并将键用作名称(而不是KeyValuePair 的集合,因为根据standard 名称应该是唯一的):

public Dictionary<string, object> AdditionalDataList { get; set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-27
    • 2019-09-13
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多