C#有两种方式序列化和反序列化JSON字符串: 第一种是System.Web.Script.Serialization下的JavaScriptSerializer,原生自带的这种方法需要与类绑定,不方便。 第二种是Json.NET第三方库方法,既可以与对象绑定,也可以反序列化为更通用的JObject,方便易用,推荐。 一: JavaScriptSerializer
using System; using System.Collections.Generic; using System.Web.Script.Serialization; namespace ConsoleAppTest { public class Studet { public string name { get; set; } public int age { get; set; } public string idCard { get; set; } public DateTime birthday { get; set; } public List<Hobby> hobbys { get; set; } } public class Hobby { public int sort { get; set; } public string desc { get; set; } } class Program { static void Main(string[] args) { string json = @"{ ""name"": ""张三"", ""age"": 20, ""idCard"": ""123456789"", ""birthday"": ""2022-10-21 00:00:00"", ""hobbys"": [ { ""sort"": 1, ""desc"": ""写字"" }, { ""sort"": 2, ""desc"": ""游泳"" } ] }"; JavaScriptSerializer jss = new JavaScriptSerializer(); // 反序列化:解析JSON字符串 Studet student = jss.Deserialize<Studet>(json); Console.WriteLine(student.birthday); // 序列化:对象转JSON字符串 string resultJson = jss.Serialize(student); Console.WriteLine(resultJson); Console.ReadKey(); } } }
二: Json.NET
using System; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ConsoleAppTest { public class Studet { public string name { get; set; } public int age { get; set; } public string idCard { get; set; } public DateTime birthday { get; set; } public List<Hobby> hobbys { get; set; } } public class Hobby { public int sort { get; set; } public string desc { get; set; } } class Program { static void Main(string[] args) { string json = @"{ ""name"": ""张三"", ""age"": 20, ""idCard"": ""123456789"", ""birthday"": ""2022-10-21 00:00:00"", ""hobbys"": [ { ""sort"": 1, ""desc"": ""写字"" }, { ""sort"": 2, ""desc"": ""游泳"" } ] }"; // 反序列化JSON字符串到对象 Studet student = JsonConvert.DeserializeObject<Studet>(json); Console.WriteLine(student.birthday); // 序列化对象到JSON字符串 string resultJson = JsonConvert.SerializeObject(student); Console.WriteLine(resultJson); Console.ReadKey(); } } }
三:JSON字符串和JObject之间相互转换:JObject.Parse
using System; using Newtonsoft.Json.Linq; namespace ConsoleAppTest { class Program { static void Main(string[] args) { string json = @"{ ""name"": ""张三"", ""age"": 20, ""idCard"": ""123456789"", ""birthday"": ""2022-10-21 00:00:00"", ""hobbys"": [ { ""sort"": 1, ""desc"": ""写字"" }, { ""sort"": 2, ""desc"": ""游泳"" } ] }"; JObject jobj = JObject.Parse(json); Console.WriteLine(jobj["birthday"].ToString()); JObject jObject = new JObject(); jObject["name"] = "张三"; jObject["birthday"] = new DateTime(2022, 10, 21, 16, 11, 9); Console.WriteLine(jObject); Console.ReadKey(); } } }
四:JSON对象数组的四种方法
对于类似于[{},{}]这种对象数组的字符串有四种方法:
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Web.Script.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ConsoleAppTest { public class Studet { public string name { get; set; } } class Program { static void Main(string[] args) { string json = @"[{""name"": ""张三""},{""name"": ""李四""}, {""name"": ""王五""}]"; // 第一种:DataTable DataTable dt = JsonConvert.DeserializeObject<DataTable>(json); Console.WriteLine(dt.Rows[0][0]); // 第二种:JavaScriptSerializer JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json); Dictionary<string, object> dic = (Dictionary<string, object>)arrayList[0]; Console.WriteLine(dic["name"]); // 第三种:JArray JArray jArray = JArray.Parse(json); Console.WriteLine(jArray[0]["name"]); // 第四种:反序列化到对象 DeserializeObject List<Studet> list = JsonConvert.DeserializeObject<List<Studet>>(json); Console.WriteLine(list[0].name); Console.ReadKey(); } } } 原文地址:https://blog.csdn.net/lilongsy/article/details/127445346
标签:set,Console,string,get,C#,JSON,using,序列化,public From: https://www.cnblogs.com/Peretsoft/p/17784174.html