C# 使用自定义类将json解析到字典中
我有这个json。 我可以将其解析为下一个对象C# 使用自定义类将json解析到字典中,c#,json,dictionary,deserialization,C#,Json,Dictionary,Deserialization,我有这个json。 我可以将其解析为下一个对象字典 如何从该对象生成类似于字典,其中 { "data" : { "TTyTo3qHkFq8b-KsIHZOlQ" : { "2016-05-01" : { "value1" : 11, "value2" : 87
字典
如何从该对象生成类似于字典
,其中
{
"data" : {
"TTyTo3qHkFq8b-KsIHZOlQ" : {
"2016-05-01" : {
"value1" : 11,
"value2" : 87
},
"2016-05-02" : {
"value1" : 2,
"value2" : 34
}
}
}
}
classcustomclass1{
日期时间日期{get;set;}
字典值{get;set;}
}
?
对于反序列化,我使用的是Json.NET。您需要实现自定义以实现您想要的
首先,您需要创建表示整个JSON的Root
类:
class CustomClass1 {
DateTime Date {get;set;}
Dictionary<string, int> Value {get;set;}
}
最后将其传递到反序列化对象
方法重载:
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; }
}
}
var root=JsonConvert.DeserializeObject(json,新的CustomClassConverter());
您发布的内容不是有效的JSON。用于验证您的JSON。@rufer7,小补丁关于数据
属性,您需要忽略它吗?@DarjanBogdan,不数据
应该存在
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; }
}
}
var root = JsonConvert.DeserializeObject<Root>(json, new CustomClassConverter());