Home > Backend Development > C#.Net Tutorial > How to solve the problem of attribute value deserialization failure?

How to solve the problem of attribute value deserialization failure?

零下一度
Release: 2017-06-23 16:22:11
Original
4319 people have browsed it

Introduction: When I was developing the webapi interface, I encountered: When a complex Json string is deserialized into an object, one of the attribute objects cannot be serialized?

Usage:

InternalRecommendRequestFormModel formData = Newtonsoft.Json.JsonConvert.DeserializeObject<InternalRecommendRequestFormModel>(dataInput);
Copy after login

Among them: InternalRecommendRequestFormModel is a complex object, and its properties contain other objects and properties. dataInput is the JSON string passed in by the interface, which is the InternalRecommendRequestFormModel object returned by another interface. Of course, some properties of the data have changed during the transmission process. It should be deserialized normally, but it is deserialized when used. An error is reported and one of the attributes cannot be serialized.

So what should we do when we encounter this kind of complex deserialization into an object?

First of all: Analysis shows that the error is caused by the serialization failure of individual attributes, so as long as this part of the attributes can be cleared or the serialized content of the attribute can be removed, it will be fine.

So: How to remove some attributes from the serialized JSON string?

The first thing we think of is string replacement, clearing, interception, etc., but it is easy to make mistakes when thinking about it. Can we turn this string into an operable object (of course not InternalRecommendRequestFormModel), then kill or clear an object and then convert it into the object we want? Actually, when I first thought about object, isn’t any object an object?

Start trying:

object formData = Newtonsoft.Json.JsonConvert.DeserializeObject<object>(dataInput);
Copy after login

The object obtained is as follows:

Discovered during debugging:

((Newtonsoft.Json.Linq.JObject)(JsonConvert.DeserializeObject(dataInput))).ChildrenTokens[46], I can get the attribute value.

In other words, as can be seen from the above code, the deserialized object object is converted into: Newtonsoft.Json.Linq.JObject object. This object is a base class of the JSON object provided by Microsoft. , that is to say, as long as you are an object, if it is serialized by json, it can help you deserialize it back.

The code is improved to:

InternalRecommendRequestFormModel formData = new InternalRecommendRequestFormModel();Newtonsoft.Json.Linq.JObject obj = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(dataInput);
obj.Remove("opinions");
formData = obj.ToObject<InternalRecommendRequestFormModel>();
Copy after login

Summary:

As long as JSON string is an object. When we directly deserialize and encounter that some attribute values ​​​​in the string do not meet the requirements, we can first convert the object to: JObject, correct the object attributes, and then use the ToObject< provided by JObject ;T>() and then convert it to the object that needs to be converted

<strong><span style="font-size: 15px">Newtonsoft.Json.Linq.JObject obj = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(dataInput);</span></strong><br>
Copy after login

The above is the detailed content of How to solve the problem of attribute value deserialization failure?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template