C# json字符串转化成Dictionary
Posted LBO.net
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# json字符串转化成Dictionary相关的知识,希望对你有一定的参考价值。
var json = @"{ ‘DisplayName‘: ‘新一代算法模型‘, ‘CustomerType‘: 1, ‘Report‘: { ‘TotalCustomerCount‘: 1000, ‘TotalTradeCount‘: 50 }, ‘CustomerIDHash‘: [1,2,3,4,5] }"; var dict = JsonConvert.DeserializeObject<Dictionary<object, object>>(json); //取值方式 Console.WriteLine(dict["DisplayName"].ToString()); var report = dict["Report"] as JObject; var totalCustomerCount = report["TotalCustomerCount"]; Console.WriteLine($"totalCustomerCount={totalCustomerCount}"); var arr = dict["CustomerIDHash"] as JArray; var list = arr.Select(m => m.Value<int>()).ToList(); Console.WriteLine($"list={string.Join(",", list)}");
以上是关于C# json字符串转化成Dictionary的主要内容,如果未能解决你的问题,请参考以下文章