JSON .NET对象映射

时间:2016-11-28 17:46:10

标签: c# json jenkins

我正在尝试反序列化使用JSON .NET从Jenkins服务器获得的JSON对象。我想将黄色突出显示的值映射到C#中的Node.NodeType字符串属性。

Property to be mapped to Node.NodeType

所以我有以下JenkinsNodeContractResolver成功映射其他属性,但不仅仅是更深层次的属性。有人可以帮我解决这个问题吗?

public class JenkinsNodesContractResolver : DefaultContractResolver
{
    private Dictionary<string, string> PropertyMappings { get; set; }

    public JenkinsNodesContractResolver()
    {
        PropertyMappings = new Dictionary<string, string>
        {
            {"DisplayName", "displayName"},
            {"Icon", "icon"},
            {"ManualLaunchAllowed", "manualLaunchAllowed"},
            {"NumExecutors", "numExecutors"},
            {"Idle", "idle"},
            {"Offline", "offline"},
            {"OfflineCauseReason", "offlineCauseReason"},
            {"TemporarilyOffline", "temporarilyOffline"},
            {"NodeType", "hudson.node_monitors.ArchitectureMonitor"}
        };
    }

    protected override string ResolvePropertyName(string propertyName)
    {
        string resolvedName;
        var resolved = PropertyMappings.TryGetValue(propertyName, out resolvedName);
        return (resolved) ? resolvedName : base.ResolvePropertyName(propertyName);
    }
}

0 个答案:

没有答案