本文介绍了序列化 .NET Dictionary<string, string>进入 JSON 键值对对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要: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" }}我无法获得所需的 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 集合的列表对象.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.因此,我建议在 Dictionary 周围丢弃外部 List,然后使用 Newtonsoft.Json.进行序列化时,JSON.Net 库中的 Converters.KeyValuePairConverter 对象: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 examplepublic 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" }} 这篇关于序列化 .NET Dictionary<string, string>进入 JSON 键值对对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 19:45