Deserialization of JSON strings into objects _ deserialization of some property values failed, json serialization

Source: Internet
Author: User

Deserialization of JSON strings into objects _ deserialization of some property values failed, json serialization

Introduction: when developing webapi interfaces, I encountered: When deserializing a complex Json string as an object, I Cannot serialize one of the attribute objects?

 

Usage:

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

InternalRecommendRequestFormModel is a complex object that contains other objects and attributes. DataInput is a JSON string passed in by the interface. It is the InternalRecommendRequestFormModel object returned by another interface. Of course, some attributes of data have changed during the transmission process and should be properly deserialized, however, in use, deserialization is an error and one of the attributes cannot be serialized.

 

So what should we do when we encounter this complicated deserialization into an object?

First, the analysis shows that the error is caused by the serialization failure of some attributes. You only need to clear or remove the serialized content of this attribute.

So: how to remove some attributes from serialized JSON strings?

 

The first thing we think of is replacement, clearing, and truncation of strings, but it is prone to errors. Can I convert this string into an operable object (not InternalRecommendRequestFormModel, of course), then destroy or clear an object and convert it to the desired object? As a matter of fact, when I think about objects, aren't all objects?

Start to try:

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

The obtained object is as follows:

During debugging, we found that:

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

That is to say, the code above shows that the deserialized object is converted to 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, it can be deserialized for you if it is serialized in json format.

 

Code improvements:

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

Summary:

As long as the JSON string is an object, when we encounter direct deserialization, some attribute values in the string do not meet the requirements, we can first convert the object to: JObject. After modifying the object attribute, use the ToObject <T> () provided by JObject to convert the object to be converted.

Newtonsoft.Json.Linq.JObject obj = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(dataInput);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.