序列化.NET字典到JSON键值对对象

时间:2022-09-15 13:50:04

I need to get:

我需要:

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; }
}

into:

成:

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

with:

:

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

I get:

我得到:

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

while, with:

同时,:

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

I get:

我得到:

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

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.

我找不到obsure http://random.url.as。我需要钥匙。我已经尝试过使用JavaScriptSerializer、DataContractJsonSerializer和自定义转换器Json。NET,所有的成功/缺点都是有限的。

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

一定有更好的方法/我忽略的东西可以让一个简单的JSON对象通过线!

1 个解决方案

#1


47  

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

首先,对于第一个例子,你基本上拥有的是KeyValuePair 对象的集合列表。 ,string>

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

它被转换成JSON的原因是

{
    "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.

至于第二个示例,您正在创建一个动态对象,其中包含一个动态对象,并且该对象的每个字段都提供了键值。

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:

因此,我建议抛弃字典 的外部列表<>,然后使用newtonsoft.json . converter。JSON中的KeyValuePairConverter对象。进行序列化时的Net库: ,string>

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

Hope that helps.

希望有帮助。

EDIT

编辑

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( ) );

This will get you the output

这会得到输出

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

#1


47  

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

首先,对于第一个例子,你基本上拥有的是KeyValuePair 对象的集合列表。 ,string>

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

它被转换成JSON的原因是

{
    "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.

至于第二个示例,您正在创建一个动态对象,其中包含一个动态对象,并且该对象的每个字段都提供了键值。

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:

因此,我建议抛弃字典 的外部列表<>,然后使用newtonsoft.json . converter。JSON中的KeyValuePairConverter对象。进行序列化时的Net库: ,string>

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

Hope that helps.

希望有帮助。

EDIT

编辑

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( ) );

This will get you the output

这会得到输出

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