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?

Jun 23, 2017 pm 04:22 PM
javascript json string object sequence

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips Mar 28, 2024 pm 03:02 PM

PHP String Operation: Remove Extra Commas and Keep Only Commas Implementation Tips In PHP development, string processing is a very common requirement. Sometimes we need to process the string to remove extra commas and retain the only commas. In this article, I'll introduce an implementation technique and provide concrete code examples. First, let's look at a common requirement: Suppose we have a string containing multiple commas, and we need to remove the extra commas and keep only the unique comma. For example, replace "apple,ba

See all articles