C# 将json序列化为具有catch all dictionary属性的对象
我想使用JSON.net反序列化到一个对象,但将未映射的属性放在dictionary属性中。可能吗 例如,给定jsonC# 将json序列化为具有catch all dictionary属性的对象,c#,json,json.net,C#,Json,Json.net,我想使用JSON.net反序列化到一个对象,但将未映射的属性放在dictionary属性中。可能吗 例如,给定json {one:1,two:2,three:3} 还有c级: 公共类映射{ 公共int One{get;set;} 公共整数二{get;set;} 公共字典rest{get;set;} } 可以将JSON.NET反序列化为值为1=1、2=1、TheRest=Dictionary{{{“three,3}}}的实例吗?您可以创建一个实例来执行您需要执行的操作。下面是一个示例(相当难
{one:1,two:2,three:3}
还有c级:
公共类映射{
公共int One{get;set;}
公共整数二{get;set;}
公共字典rest{get;set;}
}
可以将JSON.NET反序列化为值为1=1、2=1、TheRest=Dictionary{{{“three,3}}}的实例吗?您可以创建一个实例来执行您需要执行的操作。下面是一个示例(相当难看,但演示了您可能希望如何执行此操作):
名称空间JsonConverterTest1
{
公共类映射
{
私人词典_theRest=新词典();
公共int One{get;set;}
公共整数二{get;set;}
公共词典rest{get{return}
}
公共类MappedConverter:CustomCreationConverter
{
公共重写映射的创建(类型objectType)
{
返回新映射();
}
公共重写对象ReadJson(JsonReader阅读器,类型objectType,对象existingValue,JsonSerializer序列化程序)
{
var mappedObj=新映射();
var objProps=objectType.GetProperties().Select(p=>p.Name.ToLower()).ToArray();
//return base.ReadJson(reader、objectType、existingValue、serializer);
while(reader.Read())
{
if(reader.TokenType==JsonToken.PropertyName)
{
字符串readerValue=reader.Value.ToString().ToLower();
if(reader.Read())
{
if(objProps.Contains(readerValue))
{
PropertyInfo pi=mappedObj.GetType().GetProperty(readerValue,BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var convertedValue=Convert.ChangeType(reader.Value,pi.PropertyType);
pi.SetValue(mappedObj,convertedValue,null);
}
其他的
{
mappedObj.TheRest.Add(readerValue,reader.Value);
}
}
}
}
返回mappedObj;
}
}
公共课程
{
静态void Main(字符串[]参数)
{
字符串json=“{'one':1,'two':2,'three':3,'four':4}”;
mappedObj=JsonConvert.DeserializeObject(json,new MappedConverter());
Console.WriteLine(mappedObj.TheRest[“三”].ToString());
Console.WriteLine(mappedObj.TheRest[“四”].ToString());
}
}
}
因此,在反序列化JSON字符串之后,mappedObj的输出将是一个对象,其中填充了One
和Two
属性,其他所有内容都放入字典中。当然,我将一个和两个值硬编码为int
s,但我认为这说明了如何进行此操作
我希望这有帮助
编辑:我更新了代码,使其更通用。我没有对其进行全面测试,因此可能会出现一些失败的情况,但我认为它可以帮助您实现这一点。最简单的方法是使用JsonExtensionData
属性定义一个全面的字典
示例来自:
公共类目录帐户
{
//正常反序列化
公共字符串DisplayName{get;set;}
//这些属性是在OnDeserialized中设置的
公共字符串用户名{get;set;}
公共字符串域{get;set;}
[JsonExtensionData]
专用词典_附加数据;
[已序列化]
私有void已序列化(StreamingContext上下文)
{
//SAMAccountName未反序列化为任何属性
//因此,它被添加到扩展数据字典中
字符串samAccountName=(字符串)_additionalData[“samAccountName”];
Domain=samAccountName.Split('\\')[0];
UserName=samAccountName.Split('\\')[1];
}
公共目录帐户()
{
_additionalData=新字典();
}
}
字符串json=@”{
“DisplayName”:“John Smith”,
“SAMAccountName”:“contoso\\johns”
}";
DirectoryAccount=JsonConvert.DeserializeObject(json);
Console.WriteLine(account.DisplayName);
//约翰·史密斯
Console.WriteLine(account.Domain);
//康托索
Console.WriteLine(account.UserName);
//约翰
David这是一个很好的解决方案,但我希望有一个更通用的解决方案。是的,我很快就把它组合起来了。我现在无法回到它,但我很快就会让它更通用一点。它可能会涉及一点反射。但是,基本结构不会改变——只是第二个if(reader.Read())中的逻辑
block。但希望你能看到我的想法。顺便说一句,你问的问题很酷。更新了代码,使不同属性名称和/或类型的代码更通用。更新了我答案中的代码,使其更通用。
public class Mapped {
public int One {get; set;}
public int Two {get; set;}
public Dictionary<string,object> TheRest {get; set;}
}
namespace JsonConverterTest1
{
public class Mapped
{
private Dictionary<string, object> _theRest = new Dictionary<string, object>();
public int One { get; set; }
public int Two { get; set; }
public Dictionary<string, object> TheRest { get { return _theRest; } }
}
public class MappedConverter : CustomCreationConverter<Mapped>
{
public override Mapped Create(Type objectType)
{
return new Mapped();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var mappedObj = new Mapped();
var objProps = objectType.GetProperties().Select(p => p.Name.ToLower()).ToArray();
//return base.ReadJson(reader, objectType, existingValue, serializer);
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
string readerValue = reader.Value.ToString().ToLower();
if (reader.Read())
{
if (objProps.Contains(readerValue))
{
PropertyInfo pi = mappedObj.GetType().GetProperty(readerValue, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var convertedValue = Convert.ChangeType(reader.Value, pi.PropertyType);
pi.SetValue(mappedObj, convertedValue, null);
}
else
{
mappedObj.TheRest.Add(readerValue, reader.Value);
}
}
}
}
return mappedObj;
}
}
public class Program
{
static void Main(string[] args)
{
string json = "{'one':1, 'two':2, 'three':3, 'four':4}";
Mapped mappedObj = JsonConvert.DeserializeObject<Mapped>(json, new MappedConverter());
Console.WriteLine(mappedObj.TheRest["three"].ToString());
Console.WriteLine(mappedObj.TheRest["four"].ToString());
}
}
}
public class DirectoryAccount
{
// normal deserialization
public string DisplayName { get; set; }
// these properties are set in OnDeserialized
public string UserName { get; set; }
public string Domain { get; set; }
[JsonExtensionData]
private IDictionary<string, JToken> _additionalData;
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
// SAMAccountName is not deserialized to any property
// and so it is added to the extension data dictionary
string samAccountName = (string)_additionalData["SAMAccountName"];
Domain = samAccountName.Split('\\')[0];
UserName = samAccountName.Split('\\')[1];
}
public DirectoryAccount()
{
_additionalData = new Dictionary<string, JToken>();
}
}
string json = @"{
'DisplayName': 'John Smith',
'SAMAccountName': 'contoso\\johns'
}";
DirectoryAccount account = JsonConvert.DeserializeObject<DirectoryAccount>(json);
Console.WriteLine(account.DisplayName);
// John Smith
Console.WriteLine(account.Domain);
// contoso
Console.WriteLine(account.UserName);
// johns