从 NuGet 安装 Newtonsoft.Json
Install-Package Newtonsoft.Json
常用using
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
.net core
--安装 Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Add public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(); }
序列化
//序列化 对象 -> JSON 字符串 string json = JsonConvert.SerializeObject(object); //分序列化 JSON 字符串 -> 对象 var jsonObj = JsonConvert.DeserializeObject<object>(json);
通过对象直接获取字符串:复制对象,然后打开LINQPad 开,然后试用快捷键 Shfit+Alt+V 即可获取字符串。
--对象 {"code":0,"msg":"success","data":{"total":3,"details":{"codes":["1","2","3"]}}} --字符串 "{\"code\":0,\"msg\":\"success\",\"data\":{\"total\":3,\"details\":{\"codes\":[\"1\",\"2\",\"3\"]}}}"
特性
[JsonProperty("student")]//重命名属性名 [JsonIgnore]//序列化时忽略此字段 [DisplayName("学生")] //[JsonConverter(typeof(StringTruncatingConverter))] public string Student { get; set; }
JObject、JArray、JProperty、JValue 的使用
JObject用来生成一个JSON对象{}
JArray用来生成一个JSON数组[]
JProperty用来生成一个JSON数据key:value
JValue则直接生成一个JSON值
创建json对象
把JObject理解为C#中的一个类,那么JProperty就是它的属性
--案例1
var jObject = new { msg = "success", data = new { total = 1, diff = new int[3] { 1, 2, 3 } } }; Console.WriteLine(JsonConvert.SerializeObject(jObject));
输出
{ "msg": "success", "data": { "total": 1, "diff": [1, 2, 3] } }
--案例2
var jobject = new JObject(); jobject.Add(new JProperty("name", "jack")); jobject.Add(new JProperty("age", "28")); jobject.Add(new JProperty("content", new JObject(new JProperty("hobby", "game")))); Console.WriteLine(jobject);
输出
{ "name": "jack", "age": "28", "content": { "hobby": "game" } }
--案例3
var jobject = new JObject { new JProperty("name", "renee"), new JProperty("age", 30) };
输出
{ "name": "renee", "age": 30 }
--案例4
JArray array = new JArray(); array.Add("GongHui Linq"); array.Add(new DateTime(2015, 12, 14)); var jobject = new JObject(); jobject.Add(new JProperty("name", "jack")); jobject.Add(new JProperty("age", "28")); jobject.Add(new JProperty("content", new JObject(new JProperty("hobby", "game")))); jobject["myArray"] = array; var jarray = new JArray(); jarray.Add(jobject); jarray.Add(new JObject(new JProperty("name", "renne"))); Console.WriteLine(jarray);
输出
[ { "name": "jack", "age": "28", "content": { "hobby": "game" }, "myArray": [ "GongHui Linq", "2015-12-14T00:00:00" ] }, { "name": "renne" } ]
解析json对象
--案例1
var json = "{\"msg\":\"success\",\"code\":200,\"data\":\"data\"}"; var jobject = JObject.Parse(json); //获取msg对象的值 Console.WriteLine(jobject.Value<string>("msg")); Console.WriteLine(jobject["msg"]); //获取code对象的值 Console.WriteLine(jobject.GetValue("code")); //判断对象是否存在 if (jobject.ContainsKey("code") && jobject.Value<string>("code").Equals("200")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); }
输出
success
success
200
True
--案例2
//创建一个对象 var Object = new { code = 0, msg = "success", data = new { total = 3, details = new { codes = new string[] { "1", "2", "3" } } } }; //对象转字符串 var json = JsonConvert.SerializeObject(Object); //输出json Console.WriteLine(json); //解析对象 var jObject = JObject.Parse(json); var jArray = JArray.Parse(jObject["data"]["details"]["codes"].ToString()); //JArray转换对象 var list = jArray.ToObject<string[]>(); foreach (var item in list) Console.WriteLine(item);
输出
{ "code": 0, "msg": "success", "data": { "total": 3, "details": { "codes": ["1", "2", "3"] } } } 1 2 3