目錄
- 1 物體類的 Json 序列化和反序列化
- 1.1 Json 序列化
- 1.2 將不縮進的 JSON 字串轉成縮進形式
- 1.3 其他設定
- 1.4 Json 反序列化
- 2 JObject 使用
- 2.1 創建物件
- 2.2 JObject 中添加陣列
- 2.3 從 Json 字串創建 JObject
- 2.4 從 Entity 創建 JObject
- 2.5 獲取值
- 2.6 獲取陣列
1 物體類的 Json 序列化和反序列化
我們以如下的 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[] { "吃飯", "睡覺" }
};
1.1 Json 序列化
回傳不縮進的 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
}
1.2 將不縮進的 JSON 字串轉成縮進形式
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 其他設定
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": [
"吃飯",
"睡覺"
]
}
1.4 Json 反序列化
JsonConvert.DeserializeObject<Person>(json);
2 JObject 使用
2.1 創建物件
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);
2.2 JObject 中添加陣列
上例中的代碼可以簡化為:
JArray array = new JArray("吃飯", "睡覺");
2.3 從 Json 字串創建 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 創建 JObject
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 }
};
2.5 獲取值
int id;
if (obj["ID"] != null)
id = obj["ID"].Value<int>();
2.6 獲取陣列
Newtonsoft.Json.Linq 不支持直接獲取陣列,但是可以獲取 List,然后再轉化為陣列,
string[] favorites;
if (obj["Favorites"] != null)
favorites = obj["Favorites"].Value<List<string>>().ToArray();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/120907.html
標籤:C#
上一篇:C# 求Π Π/4=1-1/3+1/5-1/7+......+1/(2*n-3)-1/(2*n-1); (n=2000)
