c# - 将 JSON 反序列化为字典

标签 c# json json.net

<分区>

我想反序列化一个Json到下面的类

public class Config
{
    public String Name { get; set; }
    public SSOType Type { get; set; }
    public Dictionary<String, String> Configuration { get; set; }
}

因此,两个属性将“照常”反序列化,其余属性将被插入字典。

JsonConvert.DeserializeObject<Config>(filetext);

这会将数据推送到属性 Name 和 Type,但字典保持为空 ...

我猜 json 应该是这样的

{
    "name":"something",
    "type":"something",
    "configuration":{
        "thing1":"value",
        "thing2":"value",
        "thing3":"value"
            }
}

但我希望 json 是这样的

{
    "name":"something",
    "type":"something",
    "thing1":"value",
    "thing2":"value",
    "thing3":"value"
}

如何调整反序列化使其工作?

最佳答案

这行得通吗?

public class Config : Dictionary<string, string>
{
    public String Name { get; set; }
    public SSOType Type { get; set; }
}

关于c# - 将 JSON 反序列化为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512004/

相关文章:

c# - 结合 IdentityServer4 和 MVC 客户端

javascript - 通过对象属性访问 JSON 数组

c# - 如何通过从 c# 对象中删除空值属性和零 (0) 值属性来优化 json

c# - 无需手写 JavaScript 的 AJAX Web 应用程序

c# - 如何通过 IEnumerable 使用 TableQuery 进行查询?

java - jackson : How to ignore the POJO name while converting a POJO to JSON using Jackson?

json - 将 Json 日期时间转换为 C# 日期时间

C#:反序列化 JSON

c# - 可以安全地假设由 new 初始化的所有 C# 变量都是引用吗?

javascript - 使用 ramda 生成查询字符串