序列化.NET字典<字符串,字符串>成JSON键值对对象

问题描述:

我需要:

public class Package
{
    public Package()
    {
        name = "";
        type = new List<Dictionary<string, string>>();
    }

    public string name { get; set; }
    public List<Dictionary<string, string>> type { get; set; }
}

{
    "name":"package_name",
    "type":
    {
        "http://random.url.as.key":"random/value"
    }
}

Package package = new Package();
package.name = "package_name";
package.type.Add(new Dictionary<string, string>() { { "http://random.url.as.key", "random/value" } });

我得到:

{
    "name":"package_name",
    "type":
    [
        [
            {
                "Key":"http:\/\/random.url.as.key",
                "Value":"random\/value"
            }
        ]
    ]
}

同时,具有:

var package = new
{
    name = "package_name",
    type = new
    {
        http_random_url_as_key = "random/value"
    }
};

我得到:

{
    "name":"package_name",
    "type":
    {
        "http_random_url_as_key":"random/value"
    }
}

我不能让obsure的的http://random.url.as.key 的,我需要。我一直在使用的JavaScriptSerializer,DataContractJsonSerializer,的自定义的转换器为Json.NET,都用有限的成功/缺点尝试。

I can't get the obsure http://random.url.as.key that I need. I have tried using JavaScriptSerializer, DataContractJsonSerializer, and Custom Convertor for Json.NET, all with limited success/shortcomings.

必须有一个更好的方法/事情我俯瞰拿到过线简单的JSON对象!

There must be a better way/something I'm overlooking to get a simple JSON Object over the wire!

嗯,首先,对于第一个例子,有什么你基本上是收藏列表 KeyValuePair&LT;字符串,字符串&GT; 的对象。

Well, first off, for the first example, what you basically have is a list of collections of KeyValuePair<string,string> objects.

所以,它被转换为显示的JSON的原因是这样的:

So, the reason that it gets converted to the JSON shown is this:

{
    "name":"package_name",
    "type":
    [ // List<Dictionary<string,string>>
        [ // Dictionary<string,string>, a list of KeyValuePair<string,string> objects
            { // KeyValuePair<string,string> object 
                "Key":"http:\/\/random.url.as.key",
                "Value":"random\/value"
            }
        ]
    ]
}

至于你的第二个例子,你正在创建一个动态的对象,包含动态对象,每个对象的字段是什么所提供的核心价值。

As far as your second example, you are creating a dynamic object, containing a dynamic object, and each of the object's fields are what are providing the key value.

所以,我建议抛弃外列表与LT;&GT; 围绕词典&LT;字符串,字符串&GT; ,然后利用 Newtonsoft.Json.Converters.KeyValuePairConverter 对象在JSON.Net库做你的序列化时:

So, I would suggest ditching the outer List<> around the Dictionary<string,string>, then make use of the Newtonsoft.Json.Converters.KeyValuePairConverter object in the JSON.Net library when doing your serialization:

string json = JsonConvert.SerializeObject( package, new KeyValuePairConverter( ) );

希望有所帮助。

修改

好了,所以我想我应该给一个更具体的例子。

Okay, so I figured I should give a more concrete example

public class Package
{
    public Package()
    {
        name = "";
        type = new Dictionary<string, string>();
    }

    public string name { get; set; }
    public Dictionary<string, string> type { get; set; }
}

Package package = new Package();
package.name = "package_name";
package.type.Add("http://random.url.as.key", "random/value");
string json = JsonConvert.SerializeObject( package, new KeyValuePairConverter( ) );

这将让你的输出

{
    "name":"package_name",
    "type":
    {
        "http://random.url.as.key":"random/value"
    }
}