I have a Model like this
public class ObjectA
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public object OtherData { get; set; }
}
And Json Data Same
{
"id" : 12 ,
"name": "Ababa",
"description": "data Des",
"otherData": {
"signTime":null,
"objectId":129,
"confirmStatus": null,
"note": "nothing"
}
}
I receive this data and using JsonConvert.Deserializer by Newtonsoft.Json provider parse this Json to my object, but i after return data type IAcctionResult at controller values of them has become like this
{
"id" : 12 ,
"name": "Ababa",
"description": "data Des",
"otherData": {
"signTime":[],
"objectId":[],
"confirmStatus": [],
"note": []
}
}
Where my code was wrong ?
My result expected like receive json data (not way using parse otherData to string).
Thank guys.
2
Answers
It seems that when you deserialize the JSON to your ObjectA class and then serialize it again to return from an IActionResult, the OtherData property which is of type object is not being serialized back to JSON correctly. Instead of serializing it as a nested object, it’s serializing the properties of OtherData as arrays.
The most likely issue is with the way OtherData is handled during serialization and deserialization. Because OtherData is of type object, the serializer might be having trouble inferring the correct types for the nested properties.
Here are some steps to diagnose and solve this problem:
Check your Serialization Settings: Ensure that the serialization
settings are consistent when deserializing and serializing the
ObjectA object.
Strongly Typed OtherData: Instead of using object type for
OtherData, it’s a good practice to define a class that represents
the structure of OtherData. This way, the serializer knows exactly
how to handle it.
Custom Converter: If you must use object for OtherData, you may need
to write a custom JsonConverter that can handle serialization and
deserialization of this property properly.
Here’s a quick illustration of how you can implement the first two suggestions:
Defining a Strong Type for OtherData
Deserialization and Serialization
When you deserialize, you will do something like:
And when you serialize it back to JSON:
If you make these adjustments and your OtherData is a defined class instead of a plain object, the serializer will know how to handle the properties correctly.
However, if you still want to keep OtherData as an object due to the dynamic nature of the content, you would likely need to implement a custom JsonConverter.
The structure of the JSON object does not match the structure of your C# class ObjectA, which is the reason you are having trouble with the JSON deserialization. In particular, the JSON deserializer is interpreting certain properties in the JSON data with a value of null as arrays, although the properties in your C# class—such as int and string—are strongly typed.
Here is what is going on region:
You have a field called OtherData declared as object in your C# class ObjectA. This property is not tightly typed. The JSON serializer (like Newtonsoft.Json) attempts to translate the JSON values to the closest C# types when you deserialize the JSON.
"signTime", "objectId", and "confirmStatus" in your JSON data are null, and since OtherData is of type object, the JSON serializer is unable to determine the appropriate data types for these attributes. It views them as arrays as a result.
This problem can be fixed by creating a different class to reflect the "otherData" object’s structure, and then using that class as the type for the OtherData attribute in your ObjectA class. Here’s an illustration of how to accomplish it:
You should be able to deserialize the JSON correctly into the ObjectA class with this revised class structure and not run into empty array problems. Please ensure that the data types in the OtherData class match the real property types in your JSON data.