简体   繁体   English

使用自定义类将json解析为Dictionary

[英]Parse json into Dictionary with custom classes

{
  "data" : {
                 "TTyTo3qHkFq8b-KsIHZOlQ" : {
                       "2016-05-01" : {
                             "value1" : 11,
                             "value2" : 87
                       },
                       "2016-05-02" : {
                             "value1" : 2,
                             "value2" : 34
                       }
                 }
    }
}

I have this json. 我有这个json。 I can parse it into next object Dictionary<string, Dictionary<DateTime, Dictionary<string, int>>> 我可以将其解析为下一个对象Dictionary<string, Dictionary<DateTime, Dictionary<string, int>>>

How can I make from this object something like Dictionary<string, CustomClass1> , where 我如何从该对象中获取诸如Dictionary<string, CustomClass1> ,其中

class CustomClass1 {
    DateTime Date {get;set;}
    Dictionary<string, int> Value {get;set;}
}

?

For deserealizing I'm using Json.NET. 为了实现非现实化,我正在使用Json.NET。

You'll need to implement custom JsonConverter in order to achieve what you want. 您需要实现自定义JsonConverter才能实现所需的功能。

First, you need to create Root class which will represent whole JSON: 首先,您需要创建代表整个JSON的Root类:

public class Root
{
    public Dictionary<string, List<CustomClass1>> Data { get; set; }
}

public class CustomClass1
{
    public DateTime Date { get; set; }

    public Dictionary<string, int> Values { get; set; }
}

Please notice Data 's data type which is Dictionary<string, List<CustomClass1>> because you won't be able to deserialize into desired type: Dictionary<string, CustomClass1> 请注意Data的数据类型为Dictionary<string, List<CustomClass1>>因为您将无法反序列化为所需的类型: Dictionary<string, CustomClass1>

After that, you need to implement own custom JsonConverter : 之后,您需要实现自己的自定义JsonConverter

public class CustomClassConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        //Implement if needed
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var obj = (JObject)JObject.ReadFrom(reader);

        var root = new Root();
        root.Data = new Dictionary<string, List<CustomClass1>>();

        JObject value = (JObject)obj.GetValue("data");
        foreach (JProperty prop1 in value.Properties())
        {
            var customObjects = new List<CustomClass1>();
            foreach (JProperty prop2 in prop1.Values())
            {
                var customObject = new CustomClass1();
                customObject.Date = DateTime.Parse(prop2.Name);
                customObject.Values = new Dictionary<string, int>();
                foreach (JProperty prop3 in prop2.Values())
                {
                    customObject.Values.Add(prop3.Name, prop3.ToObject<int>());
                }
                customObjects.Add(customObject);
            }
            root.Data.Add(prop1.Name, customObjects);
        }

        return root;
    }

    public override bool CanConvert(Type t)
    {
        return t.Equals(typeof(Root)); //Converter which handles only Root types
    }

    public override bool CanRead
    {
        get { return true; }
    }
}

And finally pass it into DeserializeObject method overload: 最后将其传递给DeserializeObject方法重载:

var root = JsonConvert.DeserializeObject<Root>(json, new CustomClassConverter());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM