【问题标题】:Serialize Json object with properties dinamycally with dictionary使用字典动态序列化具有属性的 Json 对象
【发布时间】:2021-07-22 22:58:09
【问题描述】:

我需要用字典动态序列化对象的响应 留下了 json 例子 我正在尝试在 c# 类中序列化此响应对象(request_validator) 但这不起作用 有人可以帮助我,有什么想法吗?

{
    "person": {
        "testing": "CC",
        "simple": "1234545",
        "errorNames": {
            "id": "655789",
            "error": "simple"
        },
        "errorColor": {
            "id": "2",
            "error": "error color"
        }
    }
}

{
    "request_validator": [
        {
            "person.errorNames": [
                "error names"
            ],
            "person.errorColor": [
                "error color"
            ]
        }
    ]
}

public class DeserializeResponse{
      
    public Dictionary<string, List<string>> request_validator { get; set; }

}

var error = JsonConvert.DeserializeObject<List<DeserializeResponse>>(content);

【问题讨论】:

  • public List&lt;Dictionary&lt;string, List&lt;string&gt;&gt;&gt; RequestValidator { get; set; },属性名称后的第一个图表是[,用于列表/数组。所以 RequestValidator 不是字典。这是一个包含该字典的列表。
  • "request_validator" 是对象数组,而不是对象。字典通常是从对象映射的。

标签: c# json .net serialization


【解决方案1】:

您可以使用 Newtonsoft.Json 库将字符串数组中的所有属性获取到字典中

在这种情况下,您只需指向搜索到的关卡

using Newtonsoft.Json.Linq;
...
            JObject validator = JObject.Parse(content);
            IJEnumerable<JToken> validatorTokens = validator.SelectTokens("request_validator")?.Values();
            Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();
            if (validatorTokens != null)
            {
                foreach (JProperty prop in validatorTokens.Values())
                {
                    if (!errors.ContainsKey(prop.Name))
                    {
                        errors.Add(prop.Name, new List<string>());
                    }
                    errors[prop.Name].Add(prop.Value?.ToString());
                }
            }

【讨论】:

    【解决方案2】:
    public class DeserializeResponse
    {
        [JsonPropertyName("request_validator")]
        public RequestValidator[] RequestValidator { get; set; }
    }
    
    public class RequestValidator
    {
        [JsonPropertyName("person.errorNames")]
        public string[] PersonErrorNames { get; set; }
    
        [JsonPropertyName("person.errorColor")]
        public string[] PersonErrorColor { get; set; }
    }
    

    ...

    var error = JsonSerializer.Deserialize<DeserializeResponse>(content);
    

    【讨论】:

    • 嗨,胜利者,感谢您的回答,这是一个好习惯吗?因为响应错误是动态的 { [JsonPropertyName("person.errorNames")] public string[] PersonErrorNames { get;放; } [JsonPropertyName("person.errorColor")] 公共字符串[] PersonErrorColor { get;放; } [JsonPropertyName("person.errorX")] public string[] PersonErrorX { get;放; } [JsonPropertyName("person.errorY")] public string[] PersonErrorY { get;放; } [JsonPropertyName("person.errorZ")] public string[] PersonErrorZ { get;放; } }
    • 你好,谢谢你的回答,如何为RequestValidator类动态创建这些属性,以免重复代码,可以吗?
    • 如果我们不指定 json 属性,json 序列化程序将不允许我们读取值
    • 如果json中的属性是动态的,那么这种方法是行不通的,但是如果有很多属性并且它们没有改变,那么你可以使用服务(app.quicktype.io)来生成类
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    相关资源
    最近更新 更多