How to deserialize to class with variable type property using NewtonSoft Json?

Luiz Negrini

How to deserialize to an object of this type using NewtonSoft JSON? The structure contains the property type variable, but with the use of NewtonSoft based on this structure, this property would be another class.

The following code example:

public class RootProduct
{
    public Package Package {get; set;}
}

public class Package
{
    //Package has a property in which the class type should be changed in order to deserialize
    public Package_id ThisValueIsVariable {get; set;}
}

public class Package_id
{
    public string product_name {get; set;}
}

In the data query of this package it is necessary to send the Package_id, the return of this query is received as in the structure below, the field "Package_id" (in return JSON) is this code sent in the query.

Returned JSON:

"Package": {
   "Package_id": { //This is the value that changes according to the value passed in the query to JSON
       "package_name": "my product name"
   }
 }

"Package": {
   "SecondPackage_id": { //This is the value that changes according to the value passed in the query to JSON
      "package_name": "my product name"
   }
 }

"Package": {
      "ThirdPackage_id": { //This is the value that changes according to the value passed in the query to JSON
          "package_name": "my product name"
      }
 }

UPDATE:

In the above code only the first product will be deserialized, because there is only one class with the name "package_id", and the second and third JSON items will not deserialize using JsonConvert.Deserialize because the SecondPackage_id class does not exist Consecutively.

Waaghals

The solution depends if you know the name before runtime or not.

When you know the possible names before hand you can create a separate property for each type in the Package class as follows:

public class Package
{
    public Package_id FirstPackage {get; set;}
    public Package_id SecondPackage {get; set;}
    public Package_id ThridPackage {get; set;}
}

Other properties should reserialize to null.

When you do not know the possible ids at runtime then you are actually receiving a key value pair. Because they key is different per object, there technically could be multiple (even if in you case it might only always be one). Use a dictionary to get (multiple) named objects from JSON

public class RootProduct
{
    //No package class needed
    public IDictionary<string, Package_id> Package {get; set;}
}

public class Package_id
{
    public string product_name {get; set;}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Deserialize unknown primitive json property type using Gson

From Dev

deserialize type handled json using Newtonsoft library between different applications

From Dev

Deserialize to IEnumerable class using Newtonsoft Json.Net

From Dev

Newtonsoft JSON deserialize using HttpWebResponse

From Dev

Newtonsoft JSON deserialize using HttpWebResponse

From Dev

How do I deserialize a JSON array using Newtonsoft.Json

From Dev

How to update a property of a JSON object using NewtonSoft

From Dev

How to update a property of a JSON object using NewtonSoft

From Dev

Newtonsoft Deserialize to Object Store Underlying JSON as property

From Dev

C# how to deserialize my json file using Newtonsoft?

From Dev

Newtonsoft JSON deserialize to type from list

From Dev

Deserialize Json Array using Newtonsoft.Json

From Dev

C# Deserialize Json response for only one property (could't find root) using Newtonsoft.json

From Dev

How can I select an object type to deserialize using Json.Net based on property values in the data

From Dev

Newtonsoft.Json Deserialize Collection using "Add"

From Dev

How to implement a Newtonsoft JSON converter that will serialize/deserialize a complex type into a simple type

From Dev

How to deserialize list of object json by NewtonSoft?

From Dev

How to deserialize using Newtonsoft in C#

From Java

.NET NewtonSoft JSON deserialize map to a different property name

From Dev

How to serialize object to json with type info using Newtonsoft.Json?

From Dev

How to Deserialize string using Newtonsoft.Json library into custom object using C#

From Java

Newtonsoft JSON Deserialize

From Dev

Deserialize json arrays with newtonsoft

From Dev

Deserialize with Newtonsoft.Json

From Dev

Deserialize nested Json data with Newtonsoft in C# and assign into variable

From Dev

Can I deserialize Json to class like C# Newtonsoft in Python

From Dev

Deserialize JSON object with null arrays using newtonsoft.json

From Dev

How to ignore type while Deserialization using Newtonsoft.Json

From Dev

Json Deserialize to a sub class's "new" property

Related Related

  1. 1

    How to Deserialize unknown primitive json property type using Gson

  2. 2

    deserialize type handled json using Newtonsoft library between different applications

  3. 3

    Deserialize to IEnumerable class using Newtonsoft Json.Net

  4. 4

    Newtonsoft JSON deserialize using HttpWebResponse

  5. 5

    Newtonsoft JSON deserialize using HttpWebResponse

  6. 6

    How do I deserialize a JSON array using Newtonsoft.Json

  7. 7

    How to update a property of a JSON object using NewtonSoft

  8. 8

    How to update a property of a JSON object using NewtonSoft

  9. 9

    Newtonsoft Deserialize to Object Store Underlying JSON as property

  10. 10

    C# how to deserialize my json file using Newtonsoft?

  11. 11

    Newtonsoft JSON deserialize to type from list

  12. 12

    Deserialize Json Array using Newtonsoft.Json

  13. 13

    C# Deserialize Json response for only one property (could't find root) using Newtonsoft.json

  14. 14

    How can I select an object type to deserialize using Json.Net based on property values in the data

  15. 15

    Newtonsoft.Json Deserialize Collection using "Add"

  16. 16

    How to implement a Newtonsoft JSON converter that will serialize/deserialize a complex type into a simple type

  17. 17

    How to deserialize list of object json by NewtonSoft?

  18. 18

    How to deserialize using Newtonsoft in C#

  19. 19

    .NET NewtonSoft JSON deserialize map to a different property name

  20. 20

    How to serialize object to json with type info using Newtonsoft.Json?

  21. 21

    How to Deserialize string using Newtonsoft.Json library into custom object using C#

  22. 22

    Newtonsoft JSON Deserialize

  23. 23

    Deserialize json arrays with newtonsoft

  24. 24

    Deserialize with Newtonsoft.Json

  25. 25

    Deserialize nested Json data with Newtonsoft in C# and assign into variable

  26. 26

    Can I deserialize Json to class like C# Newtonsoft in Python

  27. 27

    Deserialize JSON object with null arrays using newtonsoft.json

  28. 28

    How to ignore type while Deserialization using Newtonsoft.Json

  29. 29

    Json Deserialize to a sub class's "new" property

HotTag

Archive