简体   繁体   English

如何创建像List <String,Object>这样的集合?

[英]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. 我需要创建一个像List<String,Object>这样的List<String,Object>但我似乎无法做到这一点。

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: 如果我创建一个List<Pair> ,当我必须将它序列化为JSON时我的问题就开始了,我得到类似的东西:

{
    "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. 我的猜测是我必须找到一种方法来获得List<String,Object>或控制序列化机制。 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. 此外,我不能有一个Dictionary<String,Object>因为我将重复键。 Maybe a lookup is more needed. 也许更需要查找。

Why create your own? 为什么要创建自己的? you can use KeyValuePair 你可以使用KeyValuePair

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

如果您的密钥是唯一的,您可以使用OWIN使用的内容,即IDictionary<string, object>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM