JSONConverter自定义序列化

JSONConverter自定义序列化

本文介绍了C#JSONConverter自定义序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从某个具有动态属性的API接收JSON.

I am receiving JSON from a certain API has a dynamic property.

我采用了自定义的JsonConverter方法.有没有更简单的方法来解决这个问题?

I have taken a a custom JsonConverter approach. Is there a simpler way to deal with this?

这是返回的JSON :

{
    "kind": "tm:ltm:pool:poolstats",
    "generation": 1,
    "selfLink": "https://localhost/mgmt/tm/ltm/pool/test-mypoolname_https_main/stats?ver=12.1.2",
    "entries": {
        "https://localhost/mgmt/tm/ltm/pool/test-mypoolname_https_main/~Common~test-mypoolname_https_main/stats": {
            "nestedStats": {
                "kind": "tm:ltm:pool:poolstats",
                "selfLink": "https://localhost/mgmt/tm/ltm/pool/test-mypoolname_https_main/~Common~test-mypoolname_https_main/stats?ver=12.1.2"
            }
        }
    }
}

"entries": { "https://..." }是始终变化的部分,具体取决于我对API的要求.

The "entries": { "https://..." } is that part that always changes, depending on what I request from the API.

以下是包含此信息的类结构:

public partial class PoolStatistics
{
    [JsonProperty("entries")]
    public EntriesWrapper Entries { get; set; }

    [JsonProperty("generation")]
    public long Generation { get; set; }

    [JsonProperty("kind")]
    public string Kind { get; set; }

    [JsonProperty("selfLink")]
    public string SelfLink { get; set; }


    [JsonConverter(typeof(PoolEntriesConverter))]
    public partial class EntriesWrapper
    {
        public string Name { get; set; }

        [JsonProperty("nestedStats")]
        public NestedStats NestedStats { get; set; }
    }

    public partial class NestedStats
    {
        [JsonProperty("kind")]
        public string Kind { get; set; }

        [JsonProperty("selfLink")]
        public string SelfLink { get; set; }
    }
}

通过PoolEntriesConverter上的

覆盖以反序列化:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    JObject jo = JObject.Load(reader);
    NestedStats nestedStats = (jo.First.First[NESTED_STATS]).ToObject<NestedStats>();

    EntriesWrapper entries = new EntriesWrapper
    {
        NestedStats = nestedStats,
        Name = jo.First.Path
    };

    return entries;
}

重写WriteJson(序列化)-引发异常:

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    EntriesWrapper entries = (EntriesWrapper)value;

    JObject jo = new JObject(
        new JProperty(NESTED_STATS, entries.NestedStats),
        new JProperty(entries.Name, entries.Name));
}

错误状态:

推荐答案

如果您声明类似模式

public class NestedStats
{
    public string kind { get; set; }
    public string selfLink { get; set; }
}

public class Entry
{
    public NestedStats NestedStats { get; set; }
}

public class Root
{
    public string kind { get; set; }
    public int generation { get; set; }
    public string selfLink { get; set; }
    public Dictionary<string, Entry> entries { get; set; }
}

然后您可以反序列化为

var obj = JsonConvert.DeserializeObject<Root>(json);

这篇关于C#JSONConverter自定义序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:03