本篇文章為大家展示了 Newtonsoft.Json怎么在c#項(xiàng)目中使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
我們以如下的 Person 類舉例,其中包含了常用的數(shù)據(jù)類型:
public class Person { public int ID { get; set; } public string Name { get; set; } public DateTime Birthday { get; set; } public bool IsVIP { get; set; } public float Account { get; set; } public string[] Favorites { get; set; } public string Remark { get; set; } }
創(chuàng)建一個(gè)Person
實(shí)例:
Person person = new Person { ID = 1, Name = "張三", Birthday = DateTime.Parse("2000-01-02"), IsVIP = true, Account = 12.34f, Favorites = new string[] { "吃飯", "睡覺" } };
返回不縮進(jìn)的 Json 字符串
JsonConvert.SerializeObject(person); {"ID":1,"Name":"張三","Birthday":"2000-01-02T00:00:00","IsVIP":true,"Account":12.34,"Favorites":["吃飯","睡覺"],"Remark":null}
返回縮進(jìn)的 Json 字符串
JsonConvert.SerializeObject(person, Formatting.Indented); { "ID": 1, "Name": "張三", "Birthday": "2000-01-02T00:00:00", "IsVIP": true, "Account": 12.34, "Favorites": [ "吃飯", "睡覺" ], "Remark": null }
private string JsonIndentation(string str) { //string str = JsonConvert.SerializeObject(entity); JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(str); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (obj != null) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' }; serializer.Serialize(jsonWriter, obj); return textWriter.ToString(); } else { return str; } }
或者:
private string JsonIndentation(string json) { JObject obj = JObject.Parse(json); return obj.ToString(); }
JsonSerializerSettings settings = new JsonSerializerSettings(); // 設(shè)置日期格式 settings.DateFormatString = "yyyy-MM-dd"; // 忽略空值 settings.NullValueHandling = NullValueHandling.Ignore; // 縮進(jìn) settings.Formatting = Formatting.Indented; JsonConvert.SerializeObject(person, settings);
返回:
{ "ID": 1, "Name": "張三", "Birthday": "2000-01-02", "IsVIP": true, "Account": 12.34, "Favorites": [ "吃飯", "睡覺" ] }
JsonConvert.DeserializeObject<Person>(json);
JObject obj = new JObject(); obj.Add("ID", 1); obj.Add("Name", "張三"); obj.Add("Birthday", DateTime.Parse("2000-01-02")); obj.Add("IsVIP", true); obj.Add("Account", 12.34f); // 創(chuàng)建數(shù)組 JArray array = new JArray(); array.Add(new JValue("吃飯")); array.Add(new JValue("睡覺")); obj.Add("Favorites", array); obj.Add("Remark", null);
上例中的代碼可以簡(jiǎn)化為:
JArray array = new JArray("吃飯", "睡覺");
string json = "{\"ID\":1,\"Name\":\"張三\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"吃飯\",\"睡覺\"],\"Remark\":null}"; JObject obj = JObject.Parse(json);
JObject obj = JObject.FromObject(person);
用匿名對(duì)象創(chuàng)建 JObject
JObject obj = JObject.FromObject(new { name = "jack", age = 18 }); //顯示 { "name": "jack", "age": 18 }
用初始化器
JObject obj = new JObject() { { "name", "jack" }, { "age", 18 } };
int id; if (obj["ID"] != null) id = obj["ID"].Value<int>();
Newtonsoft.Json.Linq 不支持直接獲取數(shù)組,但是可以獲取 List,然后再轉(zhuǎn)化為數(shù)組。
string[] favorites; if (obj["Favorites"] != null) favorites = obj["Favorites"].Value<List<string>>().ToArray();
上述內(nèi)容就是 Newtonsoft.Json怎么在c#項(xiàng)目中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:Newtonsoft.Json怎么在c#項(xiàng)目中使用-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.chinadenli.net/article42/gsghc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、建站公司、網(wǎng)站設(shè)計(jì)公司、商城網(wǎng)站、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容