Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to create a list like List<String,Object> but I can't seem to do this.

I created a class:

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

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

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

While I am expecting:

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

My guess is I have 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 rather than a real solution.

Also, I can't have a Dictionary<String,Object> since I will have repeated keys. Maybe a lookup is more needed.

like image 771
ashutosh raina Avatar asked Dec 22 '22 04:12

ashutosh raina


1 Answers

Why create your own? you can use KeyValuePair

List<KeyValuePair<String, Object>> nList = new List<KeyValuePair<String, Object>>();
like image 103
Shai Avatar answered Dec 29 '22 13:12

Shai