Newtonsoft.Json 常規用法
阿新 • • 發佈:2021-12-17
從 NuGet 安裝Newtonsoft.Json
Install-Package Newtonsoft.Json
常用using
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
序列化
//序列化 物件 -> JSON 字串 string json = JsonConvert.SerializeObject(object); //分序列化 JSON 字串 -> 物件 var jsonObj = JsonConvert.DeserializeObject<object>(json);
特性
[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 3TRANSLATE with x English TRANSLATE with EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back