将JSON字符串反序列化为类 [英] Deserialize JSON string into class with reflection
问题描述
我试图将一个JSON字符串反序列化成一个自定义类。我必须使用反射。我有一个字典,我序列化,发送到一个HttpPut方法,反序列化JSON字符串,并阅读字典字段。这是我到目前为止:
我把值放入字典中,像这样:
字典< string,object> valuesToUpdate = new Dictionary< string,object>();
Person p = new Person();
p.personName =OrigName;
p.age =25;
p.isAlive = true;
valuesToUpdate.Add(Person,p);
valuesToUpdate.Add(Length,64.0);
我使用JSON来序列化它:
string jsonString = JsonConvert.SerializeObject(valuesToUpdate);
然后我接受jsonString并将其发送到REST API PUT方法。 PUT方法基于字典中的Key值使用反射来更新自定义对象上的各种变量(在此示例中,我将更新customObject.Person和customObject.Length)。
PUT调用反序列化jsonString,如下所示:
string,object> newFields = JsonConvert.DeserializeObject< Dictionary< string,object>>(jsonString);
我遍历newFields并想使用反射来更新customObject的Person类。这是我的HttpPut方法读取jsonString:
[HttpPut(/ test / stuff)]
public string PutContact([FromBody] dynamic jsonString)
{
字典< string,object> newFields = JsonConvert.DeserializeObject< Dictionary< string,object>>(jsonString);
foreach(newFields中的var字段)
{
Console.WriteLine(\\\
Field key:+ field.Key);
Console.WriteLine(Field value:+ field.Value +\\\
);
PropertyInfo propInfo = typeof(Contact).GetProperty(field.Key);
类型propertyType = propInfo.PropertyType;
var value = propInfo.GetValue(contactToUpdate,null);
if(propertyType.IsClass)
{
propInfo.SetValue(contactToUpdate,field.Value,null);
}
}
}
/ p>
类型为Newtonsoft.Json.Linq.JObject的对象不能转换为类型'Person';
我也尝试使用JSON的PopulateObject方法,但它返回了这个错误:
Newtonsoft.Json.JsonSerializationException:无法将JSON对象填充到类型Person上。路径'personName',第1行....
基本上,如何获取JSON字符串,一个类(在我的例子中是'Person'类),并用反射设置它到customObject的Person字段?
> if(propertyType.IsClass)
{
propInfo.SetValue(contactToUpdate,((JObject)field.Value).ToObject(propertyType),null);
}
I'm trying to deserialize a JSON string into a custom class. I have to use reflection. I have a dictionary that I serialize, send over to an HttpPut method, deserialize the JSON string, and read the dictionary fields. Here's what I have so far:
I'm putting values into the Dictionary like this:
Dictionary<string, object> valuesToUpdate = new Dictionary<string, object>();
Person p = new Person();
p.personName = "OrigName";
p.age = "25";
p.isAlive = true;
valuesToUpdate.Add("Person", p);
valuesToUpdate.Add("Length", 64.0);
I'm using JSON to serialize it like this:
string jsonString = JsonConvert.SerializeObject(valuesToUpdate);
I then take the jsonString and send it over to a REST API PUT method. The PUT method updates various variables on a custom object based on the Key values in the dictionary using reflection (In this example I'm updating customObject.Person and customObject.Length).
The PUT call deserializes the jsonString like this:
Dictionary<string, object> newFields = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
I iterate through newFields and want to use reflection to update customObject's "Person" class. This is my HttpPut method that reads the jsonString:
[HttpPut("/test/stuff")]
public string PutContact([FromBody]dynamic jsonString)
{
Dictionary<string, object> newFields = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
foreach (var field in newFields)
{
Console.WriteLine("\nField key: " + field.Key);
Console.WriteLine("Field value: " + field.Value + "\n");
PropertyInfo propInfo = typeof(Contact).GetProperty(field.Key);
Type propertyType = propInfo.PropertyType;
var value = propInfo.GetValue(contactToUpdate, null);
if (propertyType.IsClass)
{
propInfo.SetValue(contactToUpdate, field.Value, null);
}
}
}
This generates the error:
Object of type Newtonsoft.Json.Linq.JObject' cannot be converted to type 'Person';
I've also tried using JSON's PopulateObject method but it returned this error:
Newtonsoft.Json.JsonSerializationException: Cannot populate JSON object onto type 'Person'. Path 'personName', line 1....
So basically, how can you go about taking a JSON string, converting it to a class (in my case the 'Person' class), and setting it to customObject's Person field with reflection?
if (propertyType.IsClass)
{
propInfo.SetValue(contactToUpdate, ((JObject)field.Value).ToObject(propertyType), null);
}
这篇关于将JSON字符串反序列化为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!