I am creating a json string by serializing an object in C# (using Newtonsoft) to pass to a third party charting library, so have no control over the structure I need to create. The structure requires an object with duplicate keys, something like;
{ "color": "dd3333",
"linewidth": 2,
"dataset": [ 3,4,5,6 ],
"dataset": [ 5,6,7,8]
}
I'm struggling to get the output I want. I was using a dictionary, but have just come across the need for duplicate keys, which scuppers that. I've had a look at creating my own object to support duplicate keys (starting with these suggestions Duplicate keys in .NET dictionaries?) but am still struggling to get them to serialize to the format I need.
Any suggestions would be very welcome!
To solve this, I created a dictionary object that accepted duplicate keys (copied from Duplicate keys in .NET dictionaries?), and added a JsonConverter to control how the object was serialized;
The JsonConverter object was as follows (I didn't bother with the read as I didn't need it, but it could be easily implemented);
With this defined , the following code;
gives me the json output;