将JSON字符串反序列化为类

编程入门 行业动态 更新时间:2024-10-28 07:30:16
本文介绍了将JSON字符串反序列化为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图将一个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字符串反序列化为类

本文发布于:2023-11-17 07:46:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!