How to create a collection, for example List <String, Object>?

I need to create a list like List<String,Object> , but I cannot do this.

I created a class:

 public class Pair { public String Key { get; set; } public Object Value { get; set; } } 

If I create a List<Pair> , my problem starts when I have to serialize it to JSON, I get something like:

  {
     "Key": "version",
      "Value": "3.2.1"
 },
 {
     "Key": "name",
     "Value": "coordinateOperations"
 }

While I expect:

  {
     "version": "3.2.1"
 },
 {
     "name": "coordinateOperations"
 }

I assume that I need to either find a way to have List<String,Object> , or control the serialization mechanism. The latter looks tougher and seems more like a trick than a real solution.

Also, I cannot have Dictionary<String,Object> , since I will have duplicate keys. Perhaps a search is more necessary.

+4
source share
2 answers

Why create your own? you can use KeyValuePair

 List<KeyValuePair<String, Object>> nList = new List<KeyValuePair<String, Object>>(); 
+7
source

If your keys are unique, you can use what OWIN uses, this is IDictionary<string, object>

0
source

Source: https://habr.com/ru/post/1387933/


All Articles