KB_Shayan
KB_Shayan

Reputation: 652

C# Serializing an objects into JSON but retain the name

So I'm trying to serialize a Dictionary:

Dictionary<string, List<MyClass>>

My class contains a property called MyPropery

However the string comes out as "{"List1":[{"MyProperty":[]}]}"

How would I be able to make it convert to {"List1":["MyClass":{"MyProperty":[]}]}

Upvotes: 0

Views: 52

Answers (1)

Gabriel Lima
Gabriel Lima

Reputation: 358

To achieve what you want, you would need something like:

var obj = new Dictionary<string, List<Dictionary<string, MyClass>>>();

As kalimag pointed out, ["attr": "value"] isn't a valid JSON. The serialization of the object obj, illustrated above, could yield something like:

{"List1": [{"MyClass": {"MyProperty": []}}]}

Which is a valid JSON.

Upvotes: 3

Related Questions