您好,登錄后才能下訂單哦!
本篇文章為大家展示了 Newtonsoft.Json怎么在c#項目中使用,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
我們以如下的 Person 類舉例,其中包含了常用的數據類型:
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; } }
創建一個 Person
實例:
Person person = new Person { ID = 1, Name = "張三", Birthday = DateTime.Parse("2000-01-02"), IsVIP = true, Account = 12.34f, Favorites = new string[] { "吃飯", "睡覺" } };
返回不縮進的 Json 字符串
JsonConvert.SerializeObject(person); {"ID":1,"Name":"張三","Birthday":"2000-01-02T00:00:00","IsVIP":true,"Account":12.34,"Favorites":["吃飯","睡覺"],"Remark":null}
返回縮進的 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(); // 設置日期格式 settings.DateFormatString = "yyyy-MM-dd"; // 忽略空值 settings.NullValueHandling = NullValueHandling.Ignore; // 縮進 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); // 創建數組 JArray array = new JArray(); array.Add(new JValue("吃飯")); array.Add(new JValue("睡覺")); obj.Add("Favorites", array); obj.Add("Remark", null);
上例中的代碼可以簡化為:
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);
用匿名對象創建 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 不支持直接獲取數組,但是可以獲取 List,然后再轉化為數組。
string[] favorites; if (obj["Favorites"] != null) favorites = obj["Favorites"].Value<List<string>>().ToArray();
上述內容就是 Newtonsoft.Json怎么在c#項目中使用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。