Skip to main content
Post Closed as "Duplicate" by Brian Rogers json.net
improved formatting a bit
Source Link
Brian Rogers
  • 130.4k
  • 31
  • 315
  • 315

How can I make Json.NET serializer to serialize IDictionary<,>IDictionary<,> instance into array of objects with key/value properties? By default it serializes the value of Key into JSON object's property name.

Basically I need something like this:

[{"key":"some key","value":1},{"key":"another key","value":5}]

instead of:

{{"some key":1},{"another key":5}}

I tried to add KeyValuePairConverterKeyValuePairConverter to serializer settings but it has no effect. (I found this converter is ignored for type of IDictionaryIDictionary<> but I cannot easily change the type of my objects as they are received from other libraries, so changing from IDictionary<>IDictionary<> to ICollection<KeyValuePair<>>ICollection<KeyValuePair<>> is not option for me.)

How can I make Json.NET serializer to serialize IDictionary<,> instance into array of objects with key/value properties? By default it serializes the value of Key into JSON object's property name.

Basically I need something like this:

[{"key":"some key","value":1},{"key":"another key","value":5}]

instead of:

{{"some key":1},{"another key":5}}

I tried to add KeyValuePairConverter to serializer settings but it has no effect. (I found this converter is ignored for type of IDictionary but I cannot easily change the type of my objects as they are received from other libraries, so changing from IDictionary<> to ICollection<KeyValuePair<>> is not option for me.)

How can I make Json.NET serializer to serialize IDictionary<,> instance into array of objects with key/value properties? By default it serializes the value of Key into JSON object's property name.

Basically I need something like this:

[{"key":"some key","value":1},{"key":"another key","value":5}]

instead of:

{{"some key":1},{"another key":5}}

I tried to add KeyValuePairConverter to serializer settings but it has no effect. (I found this converter is ignored for type of IDictionary<> but I cannot easily change the type of my objects as they are received from other libraries, so changing from IDictionary<> to ICollection<KeyValuePair<>> is not option for me.)

Source Link
eXavier
  • 5k
  • 4
  • 41
  • 65

Serialize Dictionary<,> as array in Json.NET

How can I make Json.NET serializer to serialize IDictionary<,> instance into array of objects with key/value properties? By default it serializes the value of Key into JSON object's property name.

Basically I need something like this:

[{"key":"some key","value":1},{"key":"another key","value":5}]

instead of:

{{"some key":1},{"another key":5}}

I tried to add KeyValuePairConverter to serializer settings but it has no effect. (I found this converter is ignored for type of IDictionary but I cannot easily change the type of my objects as they are received from other libraries, so changing from IDictionary<> to ICollection<KeyValuePair<>> is not option for me.)