0

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":[]}]}

4
  • If you need that format, I would suggest you to add a class, within it add a string property and Myclass object.
    – G_S
    Mar 8, 2019 at 9:39
  • 1
    ["name":"value"] is invalid JSON. You can't have property names in an array [], just in an object {}.
    – kalimag
    Mar 8, 2019 at 9:39
  • The JSON what you want as output is itself an invalid JSON Mar 8, 2019 at 9:43
  • Please post your code. Mar 8, 2019 at 9:45

1 Answer 1

3

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.