
JS
使用C#编程语言,我们可以将嵌套的JSON数据反序列化为嵌套的Dictionary
csharpstring JSon = @"{ ""title"": ""C#入门指南"", ""author"": { ""name"": ""张三"", ""age"": 30 }, ""publish_date"": ""2022-01-01"", ""price"": 59.99}";现在我们希望将这个JSON字符串反序列化为嵌套的Dictionarycsharpusing Newtonsoft.JSon;using System;using System.Collections.Generic;...string JSon = @"{ ""title"": ""C#入门指南"", ""author"": { ""name"": ""张三"", ""age"": 30 }, ""publish_date"": ""2022-01-01"", ""price"": 59.99}";// 反序列化为嵌套的Dictionary<string, object>对象Dictionary<string, object> book = JSonConvert.DeserializeObject<Dictionary<string, object>>(JSon);// 访问和操作数据string title = book["title"].ToString();string authorName = ((Dictionary<string, object>)book["author"])["name"].ToString();int authorAge = Convert.ToInt32(((Dictionary<string, object>)book["author"])["age"]);DateTime publishDate = Convert.ToDateTime(book["publish_date"]);double price = Convert.ToDouble(book["price"]);Console.WriteLine("书名:" + title);Console.WriteLine("作者:" + authorName);Console.WriteLine("作者年龄:" + authorAge);Console.WriteLine("出版日期:" + publishDate.ToString("yyyy-MM-dd"));Console.WriteLine("价格:" + price.ToString("0.00"));通过以上代码,我们成功将嵌套的JSON字符串反序列化为嵌套的DictionaryCopyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号