2

I'm attempting to deserialize a list of key value pairs in JSON. I've seen other answers that suggest deserializing into a dictionary, but this won't work as I have duplicate keys. I could deserialize into a list of dictionaries, but this seems silly, because each one is only one key/value pair. This also complicates accessing those keys and values.

This is a property on a class and part of a larger JSON object, so any solution that requires deserializing this chunk of JSON separately isn't going to work for me.

I've seen Deserialize array of key value pairs using Json.NET. It's a possible solution, but while I'm not opposed to writing a custom deserializer if I have to, I would prefer to use something existing if possible.

Here's an example of my data:

[{"Parcel": "Parcel_ID"}, {"Parcel": "LegalAddress"}, {"Fee": "Amount"}]

Things I've tried:

IEumerable<KeyValuePair<string, string>> Doesn't work, both key and value are null.

IEnumerable<Tuple<string, string>> Doesn't work and is awkward.

IEnumerable<IDictionary<string, string>> Works, but is awkward to work with due to needing to do FirstOrDefault().(Key|Value).

Community
  • 1
  • 1
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67

1 Answers1

-1

If you have this kind of structure many times in your project, the best is to create your own converter with Newtonsoft as say in this Post.

Community
  • 1
  • 1
OrcusZ
  • 3,555
  • 2
  • 31
  • 48