C# 对象与JSON字符串互相转换
一、JSON字符串转对象(反序列化)
1、使用Newtonsoft.Json
反序列化字符串转换为指定类型 (T)
JsonConvert.DeserializeObject<T>(json_string);
或
非数组型
JObject.Parse(json_string);
数组型使用
JArray.Parse(json_string);
2、使用System.Web.Script.Serialization
new JavaScriptSerializer().Deserialize<T>(json_string)
3、使用System.Text.Json
JsonSerializer.Deserialize<T>(json_string);
二、对象转JSON字符串(序列化)
1、使用Newtonsoft.Json
JsonConvert.SerializeObject(ObjectName);
或
JObject.FromObject(ObjectName);
2、使用System.Web.Script.Serialization
new JavaScriptSerializer().Serialize(ObjectName);
3、使用System.Text.Json
JsonSerializer.Serialize(ObjectName)

浙公网安备 33010602011771号