简体   繁体   中英

How to serialise a list of objects of different type with Newtonsoft.Json?

In an application written in c# I need to create a JSON string with a certain syntax. The data to be serialised is a list of different objects. The syntax looks like this (section of the whole string):

    "ShipmentUnit": [{
            ...
            "Services": [{
                    "Service1": {
                        "ServiceName": "service_1",
                        "Property1": "Value1",
                        "Property2": "Value2"
                    },
                    "Service2": {
                        "ServiceName": "service_2",
                        "Property3": "Value3",
                    }
                }
            ]
        }
    ]

I want to provide the data to be serialised in a suitable object structure and then serialise it with Newtonsoft.Json. Unfortunately, this does not work.

If I use an array for the list, the result looks like this:

    "ShipmentUnit": [{
            ...
            "Services": [
                    {
                        "ServiceName": "service_1",
                        "Property1": "Value1",
                        "Property2": "Value2"
                    },
                    {
                        "ServiceName": "service_2",
                        "Property3": "Value3",
                    }
                }
            ]
        }
    ]

If I use a dictionary, the result looks like this (the square brackets are missing):

    "ShipmentUnit": [{
            ...
            "Services": {
                "Service1": {
                    "ServiceName": "service_1",
                    "Property1": "Value1",
                    "Property2": "Value2"
                },
                "Service2": {
                    "ServiceName": "service_2",
                    "Property3": "Value3",
                }
            }
        }
    ]

I've already read the following topics, but they do not match my problem:

Serialise list of C# custom objects

Serialise list of objects to single JSON object, with property as key

Serialise List<Dictionary<String,Object>>

Any ideas what to do?

try this

    Data data=JsonConvert.DeserializeObject<Data>(json);
    
    json=JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented);

classes

public class Data
{
    public List<ShipmentUnit> ShipmentUnit { get; set; }
}
public class ShipmentUnit
{
    public List<Dictionary<string,Dictionary<string,string>>> Services { get; set; }
}

result

{
  "ShipmentUnit": [
    {
      "Services": [
        {
          "Service1": {
            "ServiceName": "service_1",
            "Property1": "Value1",
            "Property2": "Value2"
          },
          "Service2": {
            "ServiceName": "service_2",
            "Property3": "Value3"
          }
        }
      ]
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM