![](/img/trans.png)
[英]Serialize a Dictionary<string,Type> and remove key/value from JSON string
[英]Serialize a Dictionary with an object type key and a List type value into Json
我想將以下Dictionary類型轉換為Json:
public class Repsonse
{
public Dictionary<Employee, List<Car>> Dictionary { get; set; }
public class Employee
{
public string Name { get; set; }
public string Id { get; set; }
public decimal Seniority { get; set; }
}
public class Car
{
public string OwnerId { get; set; }
public string Model { get; set; }
public string RegistrationPlate { get; set; }
}
}
我希望它可以序列化為以下Json :
{
"Dictionary": [
{
"Name": "John Doe",
"Seniority": "2",
"OwnerId": "1111"
"Cars": [
{
"OwnerId": "1111"
"Model": "Chevrolet Spark",
"RegistrationPlate": "LTO1234"
},
{
"OwnerId": "1111"
"Model": "Chevrolet Malibu",
"RegistrationPlate": "LTO5678"
}
]
},
{
"Name": "Jane Doe",
"Seniority": "10",
"OwnerId": "9999"
"Cars": [
{
"OwnerId": "9999"
"Model": "Mercedes Benz",
"RegistrationPlate": "ABC1234"
},
{
"OwnerId": "9999"
"Model": "Mercedes Maybach",
"RegistrationPlate": "ABC5678"
}
]
}
]
}
我正在使用NewtonSoft Json進行序列化,並閱讀到我需要使用TypeConverter
來啟用這種序列化,但是它對我不起作用。
這是TypeConverter
實現:
public class Employee : TypeConverter
{
public string Name { get; set; }
public string Id { get; set; }
public decimal Seniority { get; set; }
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ((Employee)value).Name + "," + ((Employee)value).Id + "," + ((Employee)value).Seniority;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
您可以在此處查看完整的示例代碼。 我該如何運作?
您擁有的json輸出格式用於列表,而不用於字典。 如果您想使用字典,可以按照下面的一個
JSON回應
{
"Dictionary": {
"John Doe": {
"Name": "John Doe",
"Id": "1111",
"Seniority": 2,
"Cars": [
{
"OwnerId": "1111",
"Model": "Chevrolet Spark",
"RegistrationPlate": "LTO1234"
},
{
"OwnerId": "1111",
"Model": "Chevrolet Malibu",
"RegistrationPlate": "LTO5678"
}
]
},
"Jane Doe": {
"Name": "Jane Doe",
"Id": "9999",
"Seniority": 10,
"Cars": [
{
"OwnerId": "9999",
"Model": "Mercedes Benz",
"RegistrationPlate": "ABC1234"
},
{
"OwnerId": "9999",
"Model": "Mercedes Maybach",
"RegistrationPlate": "ABC5678"
}
]
}
}
}
和代碼將是
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
Response res = new Response { Dictionary = new Dictionary<string, Employee>() };
Employee firstEmployee = new Employee { Name = "John Doe", Id = "1111", Seniority = 2 };
Employee secondEmployee = new Employee { Name = "Jane Doe", Id = "9999", Seniority = 10 };
firstEmployee.Cars.Add(new Car { OwnerId = "1111", Model = "Chevrolet Spark", RegistrationPlate = "LTO1234" });
firstEmployee.Cars.Add(new Car { OwnerId = "1111", Model = "Chevrolet Malibu", RegistrationPlate = "LTO5678" });
secondEmployee.Cars.Add(new Car { OwnerId = "9999", Model = "Mercedes Benz", RegistrationPlate = "ABC1234" });
secondEmployee.Cars.Add(new Car { OwnerId = "9999", Model = "Mercedes Maybach", RegistrationPlate = "ABC5678" });
res.Dictionary.Add(firstEmployee.Name, firstEmployee);
res.Dictionary.Add(secondEmployee.Name, secondEmployee);
var result = JsonConvert.SerializeObject(res);
Console.WriteLine(result);
}
}
public class Response
{
public Dictionary<string, Employee> Dictionary { get; set; }
public class Employee : TypeConverter
{
public Employee(string name, decimal seniority, string id, List<Car> cars)
{
Name = name;
Seniority = seniority;
Id = id;
Cars = cars;
Cars = new List<Car>();
}
public Employee()
{
Cars = new List<Car>();
}
public string Name { get; set; }
public string Id { get; set; }
public decimal Seniority { get; set; }
public List<Car> Cars { get; set; }
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ((Employee)value).Name + "," + ((Employee)value).Id + "," + ((Employee)value).Seniority;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public class Car
{
public string OwnerId { get; set; }
public string Model { get; set; }
public string RegistrationPlate { get; set; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.