使用Dictionary做特殊的json字符串时(可以随意起key的名称)怎么将json字符串反序列化为json匿名对象?及匿名对象的使用方法

var object = new
{
   arr = new[] { 1, 2, 3 },
   list = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } }
};
var jsonString = JsonConvert.SerializeObject(o);
例子1:var result = JsonConvert.DeserializeAnonymousType(jsonString, new { arr= new int[0], list = new Dictionary<string, int>() });

例子2:

var dict = new Dictionary<string, IEnumerable>();
var pinYinLetter = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
foreach (var letter in pinYinLetter)
 {
   var cityList = cacheList.Where(c => c.FirstPinYin == letter).Select(c => new CityList() { name = c.Name, simple = c.SimplePinYin, text = c.SimplePinYin + " " + c.Name, value = c.PinYin });
   if (cityList != null && cityList.Count() > 0)
   dict.Add(letter, cityList);
}
var json = JsonConvert.SerializeObject(new { list = dict, status = "200" });
var result = JsonConvert.DeserializeAnonymousType(json, new { list = new Dictionary<string, IEnumerable>(), status = "200" });
return result;

 

你可能感兴趣的:(使用Dictionary做特殊的json字符串时(可以随意起key的名称)怎么将json字符串反序列化为json匿名对象?及匿名对象的使用方法)