c# Newtonsoft.Json 常用方法總結(jié)
1 實(shí)體類的 Json 序列化和反序列化
我們以如下的 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[] { "吃飯", "睡覺" }
};
1.1 Json 序列化
返回不縮進(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
}
1.2 將不縮進(jìn)的 JSON 字符串轉(zhuǎn)成縮進(jìn)形式
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();
}
1.3 其他設(shè)置
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": [
"吃飯",
"睡覺"
]
}
1.4 Json 反序列化
JsonConvert.DeserializeObject<Person>(json);
2 JObject 使用
2.1 創(chuàng)建對(duì)象
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);
2.2 JObject 中添加數(shù)組
上例中的代碼可以簡(jiǎn)化為:
JArray array = new JArray("吃飯", "睡覺");
2.3 從 Json 字符串創(chuàng)建 JObject
string json = "{\"ID\":1,\"Name\":\"張三\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"吃飯\",\"睡覺\"],\"Remark\":null}";
JObject obj = JObject.Parse(json);
2.4 從 Entity 創(chuàng)建 JObject
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 }
};
2.5 獲取值
int id; if (obj["ID"] != null) id = obj["ID"].Value<int>();
2.6 獲取數(shù)組
Newtonsoft.Json.Linq 不支持直接獲取數(shù)組,但是可以獲取 List,然后再轉(zhuǎn)化為數(shù)組。
string[] favorites; if (obj["Favorites"] != null) favorites = obj["Favorites"].Value<List<string>>().ToArray();
以上就是c# Newtonsoft.Json 常用方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于c# Newtonsoft.Json的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C# Newtonsoft.Json庫的常用屬性和方法詳解
- C#使用Newtonsoft.Json庫實(shí)現(xiàn)JSON數(shù)據(jù)中某個(gè)字段值的提取功能
- C# Newtonsoft.Json用法詳解
- C#使用Newtonsoft.Json中的JObject對(duì)象
- C# newtonsoft.json中文亂碼問號(hào)的解決方案
- C# Newtonsoft.Json 解析多嵌套json 進(jìn)行反序列化的實(shí)例
- c#添加Newtonsoft.Json包的操作
- C# Newtonsoft.Json 的使用說明
- C#下Newtonsoft.Json的具體使用
相關(guān)文章
C#向線程中傳遞多個(gè)參數(shù)的解決方法(兩種)
這篇文章主要介紹了C#向線程中傳遞多個(gè)參數(shù)的解決方法(兩種)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
WPF+ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能的示例代碼
本文以一個(gè)簡(jiǎn)單示例,簡(jiǎn)述如何通過WPF+ASP.NET SignalR實(shí)現(xiàn)消息后臺(tái)通知以及數(shù)據(jù)的實(shí)時(shí)刷新,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正2022-09-09
C#實(shí)現(xiàn)連接電子秤串口自動(dòng)稱重
這篇文章介紹了C#實(shí)現(xiàn)連接電子秤串口自動(dòng)稱重的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
Winform啟動(dòng)另一個(gè)項(xiàng)目傳值的方法
這篇文章主要介紹了Winform啟動(dòng)另一個(gè)項(xiàng)目傳值的方法,通過調(diào)用進(jìn)程來實(shí)現(xiàn)項(xiàng)目之間的傳值,需要的朋友可以參考下2014-11-11
同時(shí)兼容JS和C#的RSA加密解密算法詳解(對(duì)web提交的數(shù)據(jù)加密傳輸)
這篇文章主要給大家介紹了關(guān)于同時(shí)兼容JS和C#的RSA加密解密算法,通過該算法可以對(duì)web提交的數(shù)據(jù)進(jìn)行加密傳輸,文中通過圖文及示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07

