0
因此,我使用Dictionary类将C#序列化为JSON。使用C#字典序列化json查询字符串
我试图序列化到这个字符串
{ "User":{ "$inQuery":{ "where":{ "firstName":"plf.UserName" } } }
我试图用字典的嵌套组来组装它。像这样..
var dict4 = new Dictionary<string, string>() { {"firstName", plf.UserName} };
var dict3 = new Dictionary<string, Dictionary<string, string>>() { { "where", dict4 } };
var dict2 = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>() { { "$inQuery", dict3 } };
var dict1 = new Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>>() {{ "User", dict2 } };
当然,这不可能是最好的方式去做这件事。
我该如何做这种清洁剂?
你听说过[Newtonsoft JSON.Net](http://www.newtonsoft.com/json)吗? – Blorgbeard
我正在使用它来序列化这些嵌套字典。我如何使用核心JSON.Net库? –
您只需将对象传递给'JsonConvert.SerializeObject' - 为什么使用嵌套字典? – Blorgbeard