How do I resolve a property value deserialization failure?

Source: Internet
Author: User
Introduction: I encountered in the development of the WEBAPI interface: A complex JSON string is deserialized into the object times, unable to serialize one of the property objects?

How to use:

Internalrecommendrequestformmodel FormData = newtonsoft.json.jsonconvert.deserializeobject< Internalrecommendrequestformmodel> (Datainput);

Where: Internalrecommendrequestformmodel is a complex object that contains other objects and properties. Datainput is the interface passed in JSON string, it is another interface returned Internalrecommendrequestformmodel object, of course, the data in the process of passing some properties have changed, supposedly can be properly deserialized, But in the use of the deserialization is an error, unable to serialize one of the properties.

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

First: The analysis knows that the error is due to the failure of individual attributes to serialize, so long as you can empty this part of the property or remove the serialization content of the property, it is possible.

So: How do I remove a subset of properties from a serialized JSON string?

The first thing we think about is the substitution of strings, emptying, intercepting, and so on, but it's easy to make mistakes. Can you now turn this string into an actionable object (not Internalrecommendrequestformmodel, of course), then kill or empty an object before converting it to the object we want? In fact, I think of object, is not any object are objects?

Start trying:

Object formData = newtonsoft.json.jsonconvert.deserializeobject<object> (datainput);

Get the following object:

Found in debugging:

((Newtonsoft.Json.Linq.JObject) (jsonconvert.deserializeobject<object> (Datainput))). CHILDRENTOKENS[46], I can get the attribute value.

That is, it can be seen from the above code that the deserialized object is converted to: Newtonsoft.Json.Linq.JObject object, which is a base class for the Json object that Microsoft provided to us, that is, as long as you are an object, If it is serialized by JSON, it can help you deserialize it back.

The code improvements are:

Internalrecommendrequestformmodel formData = new Internalrecommendrequestformmodel (); Newtonsoft.Json.Linq.JObject obj = jsonconvert.deserializeobject<newtonsoft.json.linq.jobject> (datainput); o bj. Remove ("opinions"); formData = obj. Toobject<internalrecommendrequestformmodel> ();

Summarize:

As long as the JSON string is an object, when we are directly deserialized encountered, some of the property values in the string does not meet the requirements, we can first convert the object to: Jobject, after the object property correction, and then use the jobject provided by the toobject<t> () And then to the object that needs 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.